free_easy
free_easy

Reputation: 5129

Consolidate awk expressions

How can I combine this two expressions into one to be used in an awk script file?

awk '/select name="op_sys"/,/select>/' build/input.xhtml > build/output.xhtml
awk '/value="/,/">/' build/output.xhtml

Edit: I would like to transform the expressions in an awk file to look like:

#!/usr/bin/awk -f
{
   ...
}

Upvotes: 0

Views: 71

Answers (2)

William Pursell
William Pursell

Reputation: 212404

awk does not permit address ranges to be nested easily. You can do it with flags, but it's much simpler to use sed:

sed -n '/pat1/,/pat2/ { /pat3/,/pat4/p }'

will print lines between pat3 and pat4 (inclusive) only within pat1 and pat2. Note that nesting ranges like this does not behave exactly as piping the output to a second invocation, since overlaps will be treated differently.

Upvotes: 1

Steve
Steve

Reputation: 54502

I think it would be dangerous merging the two range expressions. For example, what if the second range occurs outside of (or partially overlaps) the first range? I would simply use a pipe:

< build/input.xhtml awk '/select name="op_sys"/,/select>/' | awk '/value="/,/">/'

Upvotes: 1

Related Questions