Increment value in text file

Hi Guys,

I am new to shell programing, I have a csv file which has 50k records and I have got the requirement to increment the value in second column after each 5000 records.

for example below

A,B,C,D //Header
1,1,London,UK
1,1,Manchester,UK
1,1,Glasgow,UK
.
.
.
1,1,Newyork,USA

I am expecting final output like

A,B,C,D //Header
1,1,London,UK
.
//after 5k record
.
1,2,Manchester,UK
.
//after 5k records
.
1,3,Glasgow,UK
.
.
.
.
1,10,Newyork,USA

awk -F"," -v OFS="," 'NR>1{ $2+=int((NR-1)/5000) }' infile > outfile
2 Likes

I have tried running this but it's not working for some reason.

whats the error ? Please post

I am successful with corona's piece

# cat > file
1,1,1
1,1,2
1,1,3
1,1,4
1,1,5
1,1,6
1,1,7
1,1,8
1,1,9
1,1,10
# awk -F"," -v OFS="," 'NR>1{ $2+=int((NR-1)/5) }1' file
1,1,1
1,1,2
1,1,3
1,1,4
1,1,5
1,2,6
1,2,7
1,2,8
1,2,9
1,2,10

try (modification on Corona688's solution):

awk -F"," -v OFS="," 'NR>1{ $2+=int((NR-2)/5000) } 1' infile > outfile
2 Likes

My thanks is for your name's decimal notation. Awesome !!

Thank you very much corona and rdrtx1

Brilliant this is working :slight_smile:

Can we make it conditional ? as I wanted to increment ( $2 ) value based on third column value in the file.

For example if 3rd column's value change then 2nd column value should reset to 1 again and then repeat after each 5k records and so on?

Thanks in advance

try:

awk -F"," -v OFS="," 'NR>1{ $2+=int((c)/5000); if ($3 != b) {$2=1 ; c=0} ; b=$3; c++ } 1' infile > outfile