Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extracting a field number

Status
Not open for further replies.

agilo

Programmer
Feb 4, 2004
73
TR
Hi Guys,

The following command:
gawk -v param="bts" -f key.awk inp.txt

with key.awk:

{FS = "\t"}
{ split($0,a)
}
NR==1 {
for (i=1;i<=NF;i++){
if (a == $param){
print i
break
}
}
}

results into no output, can any body help.

Thanks in advance


 
What are you trying to do? Please post sample input data and desired output.
 
Hi,
I want to extract the field number from its header (first row). A sample data is:

bs om lm bts bm .....
x1 x2 32 43 43 .....
x4 x7 54 23 32 .....
...................... .....

I expect the output to be 4, I need to pass the field name through a C- shell script to the Awk script (key.awk) to get the field number corresponds to the given name.

When I integrate the field name directly in the Awk script, I get the correct the result, otherwise I do not get any results when I use the -v option.

any comments are welcome ..

 
Try...
Code:
BEGIN {FS = "\t"}
NR==1 {
  split($0,a)
  for (i=1;i<=NF;i++){
    if (a[i] == param){
      print i
      break
      }
   }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top