awk - set numbers [ 1 ... n] from the 6 line

Hi,

i have a file, where measurement-data is stored in the first column. The file has also a header of 5 lines. I want to set counting up numbers [1, 2, 3, ...] in front of any particular measurement-value; should start at the 6. line with starting number 1.
i try to solve it with ...

awk 'NR > 6 { print FNR "\t" $0 }' file.dat

..but it will cut the header. Can someone give me a hint, how to give out the complete file?

Thanks in advance,
IMPe

Hello IMPe,

Request you to please always do show sample input and expected sample output too. If I understood correctly your requirement, could you please try following and let me know if that helps.

###If you want to leave empty line.
awk 'NR > 6 && NF{ print ++i "\t" $0 }'  Input_file
 
###If you want to count each line then following may help.
awk 'NR > 6 { print ++i "\t" $0 }'  Input_file

Thanks,
R. Singh

1 Like

Some sample always helps! Try (untested)

awk 'NR>5 {sub(/^/, NR-5)} 1' file
1 Like

Hi RavinderSingh,

thank you for the fast reply. Unfortunately your one-liner (both) work the same way and cut the top of the header like my awk-example, too. The given awk-one-liner, from the following threadpost, of RudiC work propper.

Thank you!
IMPe

---------- Post updated at 06:53 AM ---------- Previous update was at 06:51 AM ----------

Hi RudiC,

thanks a lot; Your one-liner is working propper and do exactly what i want.

Thank you!
IMPe

Hello IMPe,

Please try following it should print the headers too, not tested though.

###If you want to leave empty line.
awk 'NR==1{print;next} NR > 6 && NF{ print ++i "\t" $0 }'  Input_file
 
###If you want to count each line then following may help.
awk 'NR==1{print;next} NR > 6 { print ++i "\t" $0 }'  Input_file

Thanks,
R. Singh

The following prints in cat -n style

awk 'NR>h {printf "%6s ",NR-h} {print}' h=5 file.dat

And why don't use cat -n (or less -N , or nl ) :slight_smile:

head -n5 file; (tail -n+6 file | cat -n)