Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Nested address ranges in awk

0 views
Skip to first unread message

haakon

unread,
Nov 18, 2009, 10:14:59 AM11/18/09
to
Here's what I want to do:

I want to grab a block of text from 'from_pattern' to 'to_pattern',
and with that block of text I want to print to screen the last column
of those lines containing the secondary patterns 'local_pattern1' and
'local_pattern2'. I.e. something like this:

#
/from_pattern/,/to_pattern/ {
/local_pattern1/ printf ($NF" ")
/local_pattern2/ printf ($6"\n")
}
#

This keeps giving me syntax errors though. Is it even possible to use
nested ranges in awk or do I have to use sed for this?

Kenny McCormack

unread,
Nov 18, 2009, 10:22:39 AM11/18/09
to
In article <525cce5c-9f6b-43df...@r24g2000yqd.googlegroups.com>,

haakon <haakon...@gmail.com> wrote:
>Here's what I want to do:
>
>I want to grab a block of text from 'from_pattern' to 'to_pattern',
>and with that block of text I want to print to screen the last column
>of those lines containing the secondary patterns 'local_pattern1' and
>'local_pattern2'. I.e. something like this:
>
>#
>/from_pattern/,/to_pattern/ {
> /local_pattern1/ printf ($NF" ")
> /local_pattern2/ printf ($6"\n")
>}
>#

No. Once you are inside {}, you have to use ordinary code. There only
one level of "automatic" pattern matching.

So, the above becomes:

/from_pattern/,/to_pattern/ {
if (/local_pattern1/) printf ($NF" ")
if (/local_pattern2/) printf ($6"\n")
}

Side comments:
1) GAWK (and most "normal/standard" AWKs) allow the: if (/foo/)
syntax - i.e., a reg exp appearing by itself is shorthand for:
$0 ~ /foo/
But TAWK requires the "$0 ~" to be explicitly spelled out.

2) The usual comments about mis-use of printf().

haakon

unread,
Nov 18, 2009, 10:23:40 AM11/18/09
to
Corrected a typo..

Here's what I want to do:

I want to grab a block of text from 'from_pattern' to 'to_pattern',
and with that block of text I want to print to screen the last column
of those lines containing the secondary patterns 'local_pattern1' and
'local_pattern2'. I.e. something like this:

#
/from_pattern/,/to_pattern/ {
    /local_pattern1/                   printf ($NF" ")

    /local_pattern2/                   printf ($NF"\n")}

haakon

unread,
Nov 18, 2009, 10:26:15 AM11/18/09
to
On 18 Nov, 16:22, gaze...@shell.xmission.com (Kenny McCormack) wrote:
> In article <525cce5c-9f6b-43df-8620-92f78ed83...@r24g2000yqd.googlegroups.com>,


Thank you!

About side comment 2) What would be a better way of using printf here?

Janis Papanagnou

unread,
Nov 18, 2009, 1:10:26 PM11/18/09
to

He probably means to specify a format string

printf ("%s ", $NF)
printf ("%s\n", $6)

(But maybe he's nit-picking about the brackets, dunno.)

Janis

w_a_x_man

unread,
Nov 19, 2009, 12:09:19 PM11/19/09
to

printf ($NF" ") will crash the program if the last field is "%s".
Correct:
printf "%s", $NF

And printf ($6"\n") should simply be
print $6

Kenny McCormack

unread,
Nov 19, 2009, 3:45:41 PM11/19/09
to
In article <bff1b74e-f208-4ed0...@x31g2000yqx.googlegroups.com>,
w_a_x_man <w_a_...@yahoo.com> wrote:
...

>> About side comment 2) What would be a better way of using printf here?
>
>printf ($NF" ") will crash the program if the last field is "%s".
>Correct:
>printf "%s", $NF
>
>And printf ($6"\n") should simply be
>print $6

Give the man a cigar!

Ed Morton

unread,
Nov 19, 2009, 3:57:52 PM11/19/09
to
On Nov 19, 2:45 pm, gaze...@shell.xmission.com (Kenny McCormack)
wrote:

> In article <bff1b74e-f208-4ed0-ab80-17834aea3...@x31g2000yqx.googlegroups.com>,w_a_x_man  <w_a_x_...@yahoo.com> wrote:
>
> ...
>
> >> About side comment 2) What would be a better way of using printf here?
>
> >printf ($NF" ")  will crash the program if the last field is "%s".
> >Correct:
> >printf "%s", $NF
>
> >And  printf ($6"\n")  should simply be
> >print $6
>
> Give the man a cigar!

You might have to take it back because his reponse to the question of
"how do I use awk to skip leading fields" in another comp.lang.awk
thread was:

ruby -pne 'sub(/^\s*\S+\s+/, "")' file

;-).

Ed.

Kenny McCormack

unread,
Nov 19, 2009, 4:30:25 PM11/19/09
to
In article <bdc3a271-faa7-4124...@c3g2000yqd.googlegroups.com>,
Ed Morton <morto...@gmail.com> wrote:
...

>You might have to take it back because his reponse to the question of
>"how do I use awk to skip leading fields" in another comp.lang.awk
>thread was:
>
> ruby -pne 'sub(/^\s*\S+\s+/, "")' file
>
>;-).
>
> Ed.

Yes, I saw that. And I thought about doing a topicality flame on it,
but didn't bother.

Really, though, he just left off the AWK wrapper. He meant to say:

awk 'BEGIN {system("ruby -pne '"'"'sub(/^\s*\S+\s+/, \"\")'"'"' file")}'

0 new messages