0

I have a file with about 10k lines. I would like to add the line numbers to the first column, but in a way to repeat the line numbers 1-100. For example,

1 12313
2 3434
.
.
.
100 4233
1 24523
.
.
.
100 4543543
1
.
.
.     
100

How could I do it using grep or awk? I managed to print the line numbers, but without repeating using awk

awk '{ print NR, $1 }' p2.dat > p3.dat
  • Is the goal to add 12313 to the first 100 lines, and 3434 to second 100? Or 1 to the first 100 lines, and 2 to second 100? – agc Oct 12 '16 at 12:19
  • 1 to the first 100 lines, 2 to the second 100 lines, and so on. I managed to do it with: awk '{ for (k = 1; k <= 100; k++) if (100*(k-1) < NR && NR <= 100*k) print k, $1 }' p2.dat > p3.dat – user5694985 Oct 12 '16 at 12:26
  • @ilkkachu, well we both agree that's not so good. However, this may not be not so much a matter of modification as clarification. Here the OP had a few difficulties putting a spec into words... – agc Oct 12 '16 at 12:39
  • @user5694985, Thanks for the clarification. Looking at that the output of your awk code, it seems you wanted to prefix or prepend line numbers in incremented groups, not add numbers to input data. – agc Oct 12 '16 at 12:47
  • Rolled this back to the version that matches accepted answer. Please start a new question for the new requirements. – Kaz Oct 12 '16 at 16:58
1

Use the modulo (remainder) operator % on NR. NR % 100 would give 1,2,3...99,0,1,2..., so it needs a bit of tweaking. This should do:

awk '{ print (NR-1) % 100 + 1, $1 }' p2.dat > p3.dat

This, of course is an answer to the original question, not the modified one.

  • And what if want to add line number 1 to the first 100 lines, and number 2 to the second 100 lines and so on up to line number 100? – user5694985 Oct 12 '16 at 11:25
  • @user5694985, from that spec, it's unclear which numbers are to be added. That is if the input file was the output of seq 77 10077, did you wish for the first line of output to be 1:78 or 1:154 or something else? Please elaborate. – agc Oct 12 '16 at 11:42
  • I would like to add number 1 to the first 100 lines, number 2 to the second 100 lines, and so on: – user5694985 Oct 12 '16 at 11:46
  • I would like to add number 1 to the first 100 lines, number 2 to the second 100 lines, and so on: – user5694985 Oct 12 '16 at 11:47
  • @agc, The way I read the question, I understood the purpose was to just add the line numbers as a new field in the front of the line. Not anything having to do with numerical addition of the values. – ilkkachu Oct 12 '16 at 12:26
0

This seems to work with a minimum of looping:

awk '{ printf("%0.0f ",(NR/100.001)+.5) ;print $1 }' p2.dat > p3.dat

Demo, print the first 10 numbers, and in groups of 3 prefix an increment:

seq 10 | awk '{ printf("%0.0f ",(NR/3.001)+.5) ;print $1 }'

Output:

1 1
1 2
1 3
2 4
2 5
2 6
3 7
3 8
3 9
4 10

Note: awk rounding shows some binary conversion glitches. The above code uses expressions like (NR/3.001)+.5), when it would be clearer to write (NR/3)+.5) or even (NR*(1/3))+.5). Unfortunately those don't work:

# WRONG: (rounding errors), shows four '2's, etc.
seq 10  | awk '{ printf("%0.0f ",(NR/3)+.5) ;print $1 }' 
1 1
1 2
2 3
2 4
2 5
2 6
3 7
3 8
4 9
4 10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.