Code Comments
Programming Forum and web based access to our favorite programming groups.I have input with 7 fields. If field 6 equals X print field 2. X is numerical in this case but could be text. What's the awk syntax for this? Thanks. ~F
Post Follow-up to this messageOn 2004-12-02, Faeandar wrote:
> I have input with 7 fields. If field 6 equals X print field 2. X is
> numerical in this case but could be text.
>
> What's the awk syntax for this?
awk '$6 == X {print $2}' X=search_string
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Post Follow-up to this messageOn 2 Dec 2004 03:14:41 GMT, "Chris F.A. Johnson"
<cfajohnson@gmail.com> wrote:
>On 2004-12-02, Faeandar wrote:
>
>awk '$6 == X {print $2}' X=search_string
That's it?! Sheesh, why does the book not address that anywhere?
Thought it was a little more exotic for some reason.
If X is test does it need to be quoted?
Thanks.
~F
Post Follow-up to this messageIn article <059tq0p5ha619t1kq8dseh5iia0m2guu5v@4ax.com>,
Faeandar <mr_castalot@yahoo.com> wrote:
> On 2 Dec 2004 03:14:41 GMT, "Chris F.A. Johnson"
> <cfajohnson@gmail.com> wrote:
>
>
> That's it?! Sheesh, why does the book not address that anywhere?
> Thought it was a little more exotic for some reason.
>
> If X is test does it need to be quoted?
only if there are spaces
awk '$0 ~ X {print $2}' X='search string with spaces'
But note, I changed the test since awk parses fields by spaces.
The variable assignment should work for any awk, but most awks also allow
awk -v X='search string with spaces' '$6 == X {print $2}'
syntax, where you can have as many -v options as you like, and the
advantage here is that -v specified variables are available during the
BEGIN {...} Pattern{action} pair
Bob Harris
> Thanks.
>
> ~F
Post Follow-up to this messageOn 2 Dec 2004 03:14:41 GMT, "Chris F.A. Johnson"
<cfajohnson@gmail.com> wrote:
>On 2004-12-02, Faeandar wrote:
>
>awk '$6 == X {print $2}' X=search_string
That's it?! Sheesh, why does the book not address that anywhere?
Thought it was a little more exotic for some reason.
If X is test does it need to be quoted?
Thanks.
~F
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.