Reputation: 517
Let's say I want to match foo
and bar
in a file. The following works :
/foo/{commands}/bar/{commands}
Note: here there is no separator between /foo/{commands}
and /bar/{commands}
.
The following is also okey:
/foo/{commands1}{commands2}
where {commands2}
is executed for every line and I've left out the pattern.
But what if I want to leave out the commands? What's awk's syntax rule here? The following doesn't work:
/foo//bar/
Of course, I could write it as /foo/{print}/bar/{print}
, but I was wondering what's the delimiter for separating segments and why you sometimes need it and sometimes you don't.
Upvotes: 3
Views: 3744
Reputation: 133770
awk
works on method of regexp
then action
in this if we are mentioning /..../
and not mentioning any action so when condition is TRUE then by default print of line will happen. In case you want to put this into 2 different statements try like:
awk '/foo/;/bar/' Input_file
Above will mean like:
;
these will be treated as 2 different conditions./foo/
is true for any line then NO action is mentioned so print of that line will happen./bar/
is true for any line same thing for this also, condition is true and no action mentioned so print of line will happen.But point to be noted that in case any line has both strings in it so that line will be printed 2 times, which I believe you may NOT want it so you could do like following:
OR within single condition itself try something like:
awk '/foo|bar/' Input_file
Or in case you need to check if strings present in same line then try Like:
awk '/foo/ && /bar/' Input_file
Upvotes: 7
Reputation: 92904
To match foo
and bar
in a file - just combine patterns:
awk '/foo/ && /bar/ ....'
Upvotes: 1