Home > Archive > AWK > January 2006 > Mathing a Value and printing out a column for that value
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Mathing a Value and printing out a column for that value
|
|
| esimbo@gmail.com 2006-01-24, 7:55 am |
| Hi All
I am trying to match a value to retrieve a column and wondered if
anyone could assist me with this poser. I have three columns in a file
which, is dynamic so there's no way of knowing which column position a
search criteria will be in. The basic idea for example is to do a
search for peter and then print out the peter column
jane, peter,charles
1,2,3
4,5,6
to return
peter
2
5
The idea is to use a variable as the search pattern. I've tried
combinations of awk -v and
awk /peter/ but to no avail. Any help would really be appreciated. If
there are better alternatives, I'm willing to try these as well.
Regards
| |
| esimbo@gmail.com 2006-01-24, 7:55 am |
| The solution
File created with the following
NR == 1 {
for ( i = 1; i <= NF; i++ )
if ( $i == var )
col = i
}
{ print $col }
followed by
searchCriteria='CHF_XS'
awk -v var=$searchCriteria -F "," -f column.awk Ribit_Daily.csv
> filename.out
Thanks Jurgen
Regards
Emmon
| |
| Ed Morton 2006-01-24, 6:56 pm |
| esimbo@gmail.com wrote:
> The solution
>
> File created with the following
>
> NR == 1 {
> for ( i = 1; i <= NF; i++ )
> if ( $i == var )
> col = i
> }
> { print $col }
The above will print the whole record if no field matches "var".
There's various ways to avoid that if you want.
[color=darkred]
> followed by
>
> searchCriteria='CHF_XS'
>
> awk -v var=$searchCriteria -F "," -f column.awk Ribit_Daily.csv
>
<OT>
always quote your variables in shell, unless you have a specific reason
not to, so change the above to:
awk -v var="$searchCriteria" ...
</OT>
That's probably fine (it's what I'd recommend by default), but could
fail if $VAR contains a backslash. See question 24 in the
comp.unix.shell FAQ (http://home.comcast.net/~j.p.h/cus-faq-2.html#24)
for details.
Ed.
| |
| esimbo@gmail.com 2006-01-24, 6:56 pm |
| Hi Ed
Thanks for responding.
>The above will print the whole record if no field matches "var".
>There's various ways to avoid that if you want.
Any pointers on how to avoid this please? In theory this should not
happen but I'd like to cater for any eventualities.
> always quote your variables in shell, unless you have a specific reason not to, so
> change the above to:
Done. I've added the quotes.
| |
|
|
esimbo@gmail.com wrote:
>
> awk -v var=$searchCriteria -F "," -f column.awk Ribit_Daily.csv
Shouldn't it be
awk -v var=$searchCriteria -F "[, ]+" -f column.awk Ribit_Daily.csv
James
| |
| Ed Morton 2006-01-24, 6:56 pm |
|
esimbo@gmail.com wrote:
> Hi Ed
>
> Thanks for responding.
>
>
>
>
> Any pointers on how to avoid this please? In theory this should not
> happen but I'd like to cater for any eventualities.
You originally had:
NR == 1 {
for ( i = 1; i <= NF; i++ )
if ( $i == var )
col = i
}
{ print $col }
If you don't care about efficiency you can do this:
NR == 1 {
for ( i = 1; i <= NF; i++ )
if ( $i == var )
col = i
}
col{ print $col }
If you do care, you can do this:
NR == 1 {
for ( i = 1; i <= NF; i++ )
if ( $i == var )
col = i
}
!col { exit }
{ print $col }
You could even have written it as some variation of this (which is more
efficient than any of the solutions we've discussed):
NR == 1 {
for (col = 1; $col != var && col <= NF; col++)
;
if (col > NF)
exit
}
{ print $col }
Regards,
Ed.
|
|
|
|
|