1

I have a long file with three columns. I want to add a column (as the first column) with '5' for the first 200 lines and then '6' for the next 200 lines and so on...

  • Please edit your question and show us what your file looks like, include around 5 lines or so. – Panki May 21 at 11:04
6
awk 'BEGIN { c1 = 5 } { print c1, $0 } FNR%200 == 0 { ++c1 }' file

This would initiate a variable, c1, to 5 at the start of the awk program. All input lines are then printed prepended by the current value of this variable.

For any line whose line number (FNR) is a multiple of 200, this variable would be incremented by 1 after printing the line.

The increments to c1 would happen after printing line 200, 400, etc.

By default, the separator between the number and the original data will be a space character. Change it to a tab (or whatever you want) by setting OFS in the BEGIN block (or use -v OFS='\t' on the command line of awk):

awk 'BEGIN { OFS = "\t"; c1 = 5 } { print c1, $0 } FNR%200 == 0 { ++c1 }' file

Redirect the output to a new filename to save it.

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.