0

I had many large .txt like:

xx yy ... zz 
XX YY ... ZZ

I want to add a header row to them, each with a different number of columns:

1 2 ... 999
xx yy ... zz
XX YY ... ZZ

Since they have a different number of columns, I could not directly type "1\t2\t....", and I guess that the code should include variables like $NF. Do you have any idea? Thanks for your help.

Romeo Ninov
  • 17,484

1 Answers1

2

Use awk:

awk 'NR==1{for (i=1;i<=NF;i++) printf "%s%s",i,i==NF?ORS:OFS}1' file

Add BEGIN{FS=OFS="\t"} at the beginning of your awk script if you have a tab-delimited file.

Check here how to edit your file inplace.

pLumo
  • 22,565