Difference between /text/ and "text" in awk

Why does this search give different result in awk
I do see a mix of this in the example around the net.
What to use and why?

data

1 = red
2 = green
3 = blue
4 = black
awk '$3 == /blue/' data
"no result"
awk '$3 == "blue"' data
3 = blue
awk '$3 ~ /blue/' data
3 = blue
awk '$3 ~ "blue"' data
3 = blue

If you want to check the exact string == opeator will be the better choice.
If you want to check the part of the string then pattern match would be better and has more variety of options and more powerful.

Cheers,
Ranga:)

The /.../ denotes a regular expression, which can include wildcards etc., against $3 will be matched. The matching operator is ~
The "..." denotes an exact string that can be matched, or can be used for an equality check (operator == ).

Actually, while staring at it, I can't see why the == doesn't like the regex.

@rangarasan:
Here you are checking the exact string "/blue/" instead of "blue" with the third column.
Sorry, I disagree. Exact string needs to be double quoted. So I think it still is a regex that awk doesn't like in that place.

Thanks, I will try to read and understand what you are saying :wink:

If I search for a string, its better to use ".." and not /../ to make sure it works?

awk '$3 ~ /blue|black/'
awk '$3 ~ "blue|black"'
gives
3 = blue
4 = black

but
awk '$3 == /blue|black/'
awk '$3 == "blue|black"'
gives nothing

I still does not see why == does not work, it should eval the information and test one bye one and give result

The | is used as an or operator when matching regexs ONLY. If you try == on it, it searches for the exact string including | in your input.
I think you need to understand the difference between "matching" and "comparing".

This explain it. == is much more strict, so if you like to search for multiple information or regex, use ~

And with ~ if i search for simple text with some variance use ".."
For advanced regex use /../

The only meaningful difference is with the ~ operator ( / .. / cannot be used with the == operator )
There is a difference between " .. " (string interpreted as ERE) and / .. / (ERE constant)

Compare:

$ echo '|a' | awk '$0~/\|a/' 
|a

$ echo '|a' | awk '$0~"\|a"' 
awk: illegal primary in regular expression |a at a

$ echo '|a' | awk '$0~"\\|a"' 
|a

" .. " allows for concatenations with variables, whereas / .. / does not...

2 Likes

Within gsub, ther is no problem to use both?

awk '{gsub(/blue/,"orange")};1' data
awk '{gsub("blue","orange")};1' data

The first finds regex that contains blue
the second finds the string blue
correct?

There is no problem, but the same difference applies between " .. " and / .. /
So that is not correct: the first matches the regex that contains blue and the second one as well...

So in short:

$3 == /blue/   # has no meaning, is not valid syntax
$3 == "blue"   # exact string comparison
$3 ~ /blue/    # regex match (ERE constant)
$3 ~ "blue"    # regex match (string interpreted as ERE)