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?
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().
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")}
Thank you!
About side comment 2) What would be a better way of using printf here?
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
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.
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")}'