0

I am have the below CSV file

vol123,12/05/2016,ABC
vol123,12/05/2016,ABC
vol123,12/05/2016,ABC
vol123,14/05/2016,ABC
vol123,13/05/2016,ABC
vol123,12/05/2016,ABC
vol123,12/05/2016,ABC
vol123,18/05/2016,ABC
vol123,12/05/2016,ABC
vol123,12/05/2016,ABC
vol123,12/05/2016,ABC
vol123,12/05/2016,ABC
vol123,19/05/2016,ABC
vol123,12/05/2016,ABC
vol123,12/05/2016,ABC
vol123,20/05/2016,ABC
vol123,12/05/2016,ABC
vol123,12/05/2016,ABC
vol123,22/05/2016,ABC
vol123,12/05/2016,ABC
vol123,12/05/2016,ABC

I want to change this as below,

vol123,12/05/2016,ABC1
vol123,12/05/2016,ABC2
vol123,12/05/2016,ABC3
vol123,14/05/2016,ABC4
vol123,13/05/2016,ABC5
vol123,12/05/2016,ABC6
vol123,12/05/2016,ABC7
vol123,18/05/2016,ABC8
vol123,12/05/2016,ABC9
vol123,12/05/2016,ABC10
vol123,12/05/2016,ABC1
vol123,12/05/2016,ABC2
vol123,19/05/2016,ABC3
vol123,12/05/2016,ABC4
vol123,12/05/2016,ABC5
vol123,20/05/2016,ABC6
vol123,12/05/2016,ABC7
vol123,12/05/2016,ABC8
vol123,22/05/2016,ABC9
vol123,12/05/2016,ABC10
vol123,12/05/2016,ABC1

I want to a append number to last column. Number range is 1 to 10. I want 1 again on the 11th record so append should work in loop.

Please help me as I am unable to get this using awk command.

1 Answer 1

5

You can use awk to append the line number (NR) to each line with:

awk '{print $0 NR}' file

If you want to print the remainder when the line number is divided by 10:

awk '{print $0 NR%10}' file

That prints what you want except it prints 0 instead of 10. If you need 10, you can calculate (NR - 1)%10 + 1:

awk '{print $0 (NR - 1)%10 + 1}' file

Result:

vol123,12/05/2016,ABC1
vol123,12/05/2016,ABC2
vol123,12/05/2016,ABC3
vol123,14/05/2016,ABC4
vol123,13/05/2016,ABC5
vol123,12/05/2016,ABC6
vol123,12/05/2016,ABC7
vol123,18/05/2016,ABC8
vol123,12/05/2016,ABC9
vol123,12/05/2016,ABC10
vol123,12/05/2016,ABC1
vol123,12/05/2016,ABC2
vol123,19/05/2016,ABC3
vol123,12/05/2016,ABC4
vol123,12/05/2016,ABC5
vol123,20/05/2016,ABC6
vol123,12/05/2016,ABC7
vol123,12/05/2016,ABC8
vol123,22/05/2016,ABC9
vol123,12/05/2016,ABC10
vol123,12/05/2016,ABC1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .