Reputation: 1115
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?
Upvotes: 35
Views: 115692
Reputation: 140257
The POSIX way
awk '/pattern1/ || /pattern2/{print}'
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/
Upvotes: 45
Reputation: 284786
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.
Upvotes: 28