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}'

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/


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.

awk '/PATT1|PATT2/ { print }'

why dont you want to use grep?

grep -e 'pattern1' -e 'pattern2'

Need Your Help

update query with join on two tables

sql postgresql

I have customer and address tables.

Utf8_general_ci or utf8mb4 or...?

utf-8 localization utf-16 utf-32 utf8mb4

utf16 or utf32? I'm trying to store content in a lot of languages. Some of the languages use double-wide fonts (for example, Japanese fonts are frequently twice as wide as English fonts). I'm no...