假设以下输入:
$ cat example
{many lines of text}
Col1 Col2 Col3
foo bar 2
bar baz 3
baz bar 8
bar foo 0
foo baz 9
baz bar 3
{many more lines of text}
Run Code Online (Sandbox Code Playgroud)
以下两个awk片段解析了我之后的数据:
cat example | awk -v 'RS=\n\n' '/^Col1 /' | awk '$2 == "bar" && $3 > 1 {print $1}'
foo
baz
baz
Run Code Online (Sandbox Code Playgroud)
如何将两个片段组合成一个awk,例如
awk '
...
...
...
' example
Run Code Online (Sandbox Code Playgroud)
你可以做:
awk '/^Col1 /,/^$/{ if( $2 == "bar" && $3 > 1 ) print $1}' example
Run Code Online (Sandbox Code Playgroud)
这似乎有效.
gawk '/^$/{getline;if(/^Col1/){doit=1}else{doit=0;}} doit && $2=="bar" && $3>1 {print $1}' example
Run Code Online (Sandbox Code Playgroud)
分成带有注释的可读块,这是:
/^$/ { # Look for a blank line
getline; # Get the next line
if (/^Col1/) { # See if your column heads exist.
doit=1 # If they do, set a boolean to true
} else {
doit=0; # Otherwise, false.
}
}
doit && $2=="bar" && $3>1 { # Check the boolean AND your conditions, then
print $1 # print.
}
Run Code Online (Sandbox Code Playgroud)