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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How would I grab data values from a

Status
Not open for further replies.

ranjit

Technical User
Apr 14, 2000
131
GB
How would I grab data values from a column formatted file with headings?

Example data file:

Col1 Col2 Col3 Col4 Col5 MEM6 Col7 Col8
45 44 100 12 333 999 12 11

I'm interested in grabbing the value for MEM6 - any ideas as to how to achieve this?

Thanks in advance,
 

nawk -f grabColumn.awk file.txt

#------------------- grabColumn.awk
BEGIN {
col2grab="MEM6"
}

FNR==1{
for(i=1; i <= NF; i++)
header[$i]=i

if (! (col2grab in header)) {
printf(&quot;ERROR: [%s] is NOT one of the valid columns\n&quot;, col2grab);
exit 1
}
next;
}

{
print $header[col2grab]
} vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
alternative:

awk '{ if(NF >1) print $6; }' filename
you can also perform computing of them
awk '{ if(NF >1) print a+=$6; }' filename -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top