Awk: single quote match in If

Hello,

I'd like to print line if column 5th doesn't match with exm. But to reach there I have to make sure I match single quote.
I'm struggling to match that.

I've input file like:

Warning: Variants 'exm480340' and '5:137534453:G:C' have the same position.
Warning: Variants 'exm480345' and '5:137536897:C:G' have the same position.
Warning: Variants 'exm2274094' and '5:137548976:G:C' have the same position.
Warning: Variants 'exm480367' and '5:137548988:G:A' have the same position.
Warning: Variants 'exm480373' and '5:137588691:G:A' have the same position.
Warning: Variants 'exm480383' and '5:137589456:G:A' have the same position.
Warning: Variants 'exm480395' and '5:137593337:G:A' have the same position.
Warning: Variants 'exm480405' and '5:137593620:A:G' have the same position.
Warning: Variants 'exm2087676' and '5:137599964:C:T' have the same position.
Warning: Variants 'exm480435' and '5:137621421:C:T' have the same position.

Warning: Variants 'exm2256415' and 'exm2067772' have the same position.

Code:

grep " have the same position." input.log | awk ' { if( $5 ~ /^\'/ ){print $0} }  '

Error:

-bash: syntax error near unexpected token `)'

I tried with "'" too, but it failed.
How do I get around this error?

Edit: Sure, I'll use CODE for data.
I've added line for which I'm writing awk line.

Not clear. How should '5:137534453:G:C' "match with exm"?

For single quotes, you could e.g. define a variable outside the awk script.

Hi Rudic,

Sorry, I've edited my post. Missed the case I'm intending to use awk and if.

Try

awk -vSQ="'" '
/have the same position./ && 
$5 ~ "^" SQ
' file
grep " have the same position." input.log | awk ' { if( $5 ~ /^\x027/ ){print $0} }  '

Thanks.
It frustrates me how simple things don't work on awk/bash.


 grep " have the same position." input.log | awk ' { if( $5 ~ /^\x027exm/ ){print $0} }  '

I added exm to \x027
If I added "exm" again code fails.

Another slight variation

awk -v re="^'" '/ have the same position\./ && $5~re' file

@Scrutinizer:

Thanks. This one works as needed

grep " have the same position." input.log | awk -v re="^'exm" ' { if (! ( $5  ~ re )) print $0 }'

It's hard for me not use if and parentheses. I don't know how you folks easily swim around without if, and other syntax. :slight_smile:
I added ! in if because I need only those lines that don't have 'exm in fifth column.

Edited: for more details.

The proper (portable) escape for a ' within a ' ' string is

echo 'a '\'' character'

Explanation: the \' must be outside the ' ' string, so the latter is split into two parts with the \' in between.

echo 'a '\'' character'
grep " have the same position." input.log | awk ' { if( $5 ~ "^\x027" "exm" ){print $0} }  '