Re: Refer to field headings?





On 11/27/2007 2:30 PM, Sashi wrote:
Hi all,

I have a comma delimited text file which I'm parsing with awk. The
first line consists of field names and the rest of the lines have the
values.

Is it possible in [g]awk to refer to the field names instead of
positions?

For example,
awk '( $4 ~ /ASPAC/) {do something;}' < input_file

awk '( region_name ~ /ASPAC/) {do something;}' < input_file

where region_name is the title for the fourth field.

Thanks,
Sashi

I think what you want is something like this:

awk 'NR==1 {for (i=1;i<=NF;i++) f[$i]=i; next}
$f["region_name"] ~ /ASPAC/) {do something}' input_file

Regards,

Ed.

.



Relevant Pages

  • Re: Refer to field headings?
    ... Sashi wrote: ... I have a comma delimited text file which I'm parsing with awk. ... first line consists of field names and the rest of the lines have the ...
    (comp.lang.awk)
  • Refer to field headings?
    ... I have a comma delimited text file which I'm parsing with awk. ... first line consists of field names and the rest of the lines have the ... Is it possible in awk to refer to the field names instead of ...
    (comp.lang.awk)
  • Re: awk quries
    ... Example there is a file called data.txt consists of: ... How to go about it with awk? ... >> Is it possible to print a single record instead of all records in a file ... Prev by Date: ...
    (comp.lang.awk)
  • Re: Refer to field headings?
    ... I have a comma delimited text file which I'm parsing with awk. ... first line consists of field names and the rest of the lines have the ... to reference those fields (the variables require more typing at initialisation, ...
    (comp.lang.awk)