knowone
knowone

Reputation: 840

parsing tab separated header of a file in unix

I'm trying to work out a generic script for getting the tab separated column values (as a header to the file). Separating via awk is fine for getting the column names. I'm not getting the idea as how to get each tab separated values until the end or the last NF(if using awk). The number of columns in the file isn't fix, sometime it might come up with 20 columns, sometime it could be 100, etc. For ex: the tab separated columns in the file are-

abc ttr nnc r32 inc ...

If I write a simple awk as:

head -1 file | awk 'BEGIN {NF="\t"} {print $1,$2, etc}

It'd present each tab separated column represented by $1, $2, etc. I tried an incremental version by replacing $1, $2, etc by $i but wouldn't work.

Any ideas on this?

Upvotes: 1

Views: 562

Answers (1)

John1024
John1024

Reputation: 113844

If I understand correctly, you are asking how to loop over the fields from 1 to NF. Here is an example of such a loop:

$ head -1 file | awk -F"\t" '{for (i=1;i<=NF;i++)printf "%s ",$i; print"";}'
abc ttr nnc r32 inc 

Upvotes: 1

Related Questions