I need to search multiple patterns on the third column of a file, and then print the whole line.
I am using this one below but how do I get it to print the whole line where there is a match?
awk '{print $3}' file | egrep -w "S|M|D"
I need to search multiple patterns on the third column of a file, and then print the whole line.
I am using this one below but how do I get it to print the whole line where there is a match?
awk '{print $3}' file | egrep -w "S|M|D"
I think your requirement just needs awk and not a combination with grep. If you are looking to print the whole line where the third column matches any of those letters, you need to do
awk '$3 ~ /^(S|M|D)$/' file
[SMD]
grep -w does?
To extract the lines whose 3rd whitespace-delimited field is exactly S, M or D, use one of
awk '$3 ~ /^[SMD]$/' file
or, using string matching rather than regular expression matching,
awk '$3 == "S" || $3 == "M" || $3 == "D"' file
A condition without a corresponding block will act as if its block simply was { print }.
awk '$3 ~ /^S/||/^M/||/^D/{print $0}' filename