Reputation: 43
I want to print only the lines that have specific string say "fgfgfg" in column 3 or column 4 and column 2 start with fg I'm using the following command but it also prints lines which doesn't have fg as the starting characters in column 2?
awk '{if (($3 == "fgfgfg" || $4 == "fgfgfg") && $2 ~ /fg/) print $0}' file_name
the must condition is that column $2 must start with 'fg' and I also want to add a condition that column 2 start with fg and followed by 5 digits? any help please!!
Upvotes: 1
Views: 136
Reputation: 157947
To make sure that column 2 starts with fg
you need to use:
$2 ~ /^fg/
note the ^
. It is the start of the line anchor. If you omit it the regex would match fg
at any position within the string.
Btw, you can simplify the command:
awk '(($3 == "fgfgfg") || ($4 == "fgfgfg")) && $2 ~ /^fg/' file
Upvotes: 1
Reputation: 203209
awk '(($3 == "fgfgfg") || ($4 == "fgfgfg")) && ($2 ~ /^fg[0-9]{5}/)' file_name
Upvotes: 1