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 sed command will, by default, print the pattern space at the end of each cycle. However, in this example, we only want to ask sed to print the lines we need. Therefore, we've used the -n option to prevent the sed command from printing the pattern space. Instead, we'll control the output using the p command.
awk '{ print $2; }' prints the second field of each line. This field happens to be the process ID from the ps aux output. xargs kill -${2:-'TERM'} takes the process IDs from the selected sidekiq processes and feeds them as arguments to a kill command.
Original answer: Append printf "\n" at the end of each awk action {} . printf "\n" will print a newline.
Any awk expression is valid as an awk pattern. The pattern matches if the expression's value is nonzero (if a number) or non-null (if a string). The expression is reevaluated each time the rule is tested against a new input record.
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/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With