开发者

Using sed/awk to print lines with matching pattern OR another matching pattern

I need to print lines in a file matching a pattern OR a different pattern using awk or sed. I feel like this is an easy task but I can't seem to find an answer. 开发者_如何转开发Any ideas?


The POSIX way

awk '/pattern1/ || /pattern2/{print}'

Edit

To be fair, I like lhf's way better via /pattern1|pattern2/ since it requires less typing for the same outcome. However, I should point out that this template cannot be used for logical AND operations, for that you need to use my template which is /pattern1/ && /pattern2/


Use:

sed -nr '/patt1|patt2/p'

where patt1 and patt2 are the patterns. If you want them to match the whole line, use:

sed -nr '/^(patt1|patt2)$/p'

You can drop the -r and add escapes:

sed -n '/^\(patt1\|patt2\)$/p'

for POSIX compliance.


why dont you want to use grep?

grep -e 'pattern1' -e 'pattern2'


awk '/PATT1|PATT2/ { print }'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜