CuriousMind
CuriousMind

Reputation: 15798

awk: use field names as column specifiers

awk typically uses $1, $2 to specify the columns. Suppose there is a header line with field names for the columns, is there a way to specify the column by using the field names, not the column counts?

e.g., for this file

ID, Name, Salary
1, John, 100000

instead of using $1, $2, $3, can I somehow use things like $ID, $Name, $Salary...

Of course, I understand that there is no direct way of using $ID. I am just curious if there is a similar and easy way. Some csv files have 100+ columns and using the field name makes it much much easier.

Upvotes: 1

Views: 200

Answers (1)

jaypal singh
jaypal singh

Reputation: 77095

This script taken from awk.info authored by Janis Papanagnou might help.

awk -F, -v cols="${1:?}" '
   BEGIN {
     n=split(cols,col)
     for (i=1; i<=n; i++) s[col[i]]=i
   }
   NR==1 {
     for (f=1; f<=NF; f++)
       if ($f in s) c[s[$f]]=f
     next
   }
   { sep=""
     for (f=1; f<=n; f++) {
       printf("%c%s",sep,$c[f])
       sep=FS
     }
     print ""
   }
'

Upvotes: 1

Related Questions