awk '/pattern/' filename
is equivalent to
grep pattern filename
But awk is more powerful than grep, in that we can display only specific fields from the matching lines.
The command
awk '/pattern/ {print $2}' filename
will only print the second field of the matching lines.
Patterns can be combined with the &&, || and ! operators.
awk '/foo/ && /bar/ {print $2}' example.txt
awk '/foo/ && !/bar/ {print $2}' example.txt
The first command above will print the second field of the lines in example.txt that contain both 'foo' and 'bar', while the second one will only match the lines that contain 'foo' but not 'bar'.
No comments:
Post a Comment