For Programmers: Free Programming Magazines  


Home > Archive > AWK > December 2004 > field question









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 field question
Faeandar

2004-12-02, 3:55 am

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
Chris F.A. Johnson

2004-12-02, 3:55 am

On 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
Faeandar

2004-12-02, 3:55 am

On 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
Bob Harris

2004-12-02, 3:56 pm

In 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

Faeandar

2004-12-07, 3:59 am

On 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com