Visit Our UNIX and Linux User Community


Difference between /text/ and "text" in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difference between /text/ and "text" in awk
# 1  
Old 01-25-2013
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
Code:
1 = red
2 = green
3 = blue
4 = black

Code:
awk '$3 == /blue/' data
"no result"

Code:
awk '$3 == "blue"' data
3 = blue

Code:
awk '$3 ~ /blue/' data
3 = blue

Code:
awk '$3 ~ "blue"' data
3 = blue

# 2  
Old 01-25-2013
Quote:
Originally Posted by Jotne
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
Code:
1 = red
2 = green
3 = blue
4 = black

Here you are checking the exact string "/blue/" instead of "blue" with the third column.
Code:
awk '$3 == /blue/' data
"no result"

Here you are checking the exact string "blue" with the third column. If $3 have "blue ", It wont be matched with this.
Code:
awk '$3 == "blue"' data
3 = blue

Here you are checking that the string "blue" is present in the third column? If $3 have "blue ", It will be matched with this. This is pattern matching operator.

Code:
awk '$3 ~ /blue/' data
3 = blue

Here you are checking that the string "blue" is present in the third column? If $3 have "blue ", It will be matched with this. This is pattern matching operator.

Code:
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,
RangaSmilie
# 3  
Old 01-25-2013
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.

Last edited by RudiC; 01-25-2013 at 06:04 AM..
# 4  
Old 01-25-2013
Thanks, I will try to read and understand what you are saying Smilie

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

Code:
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

Last edited by Jotne; 01-25-2013 at 06:15 AM..
# 5  
Old 01-25-2013
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".
# 6  
Old 01-25-2013
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 /../
# 7  
Old 01-25-2013
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:

Code:
$ 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...

Last edited by Scrutinizer; 01-25-2013 at 08:10 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #977
Difficulty: Medium
A Linux Kernel Code of Conflict was introduced on 8 March 2012.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk: Performing "for" loop within text block with two files

I am hoping to pull multiple strings from one file and use them to search within a block of text within another file. File 1PS001,001 HLK PS002,004 MWQ PS004,002 RXM PS004,006 DBX PS004,006 SBR PS005,007 ML PS005,009 DBR PS005,011 MR PS005,012 SBR PS006,003 RXM PS006,003 >SJ PS006,010... (11 Replies)
Discussion started by: jvoot
11 Replies

2. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

3. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

4. UNIX for Dummies Questions & Answers

awk - difference between -F"," and BEGIN{FS=","}

in awk, what is the difference between: -F"," and BEGIN{FS=","} (2 Replies)
Discussion started by: locoroco
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Extract text between two specified "constant" texts using awk

Hi All, From the title you may know that this question has been asked several times and I have done lot of Googling on this. I have a Wikipedia dump file in XML format. All the contents are in one XML file i.e. all different topics have been put in one XML file. Now I need to separate them and... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

8. Shell Programming and Scripting

read -p "prompt text" foo say "read: bad option(s)" in Bourne-Shell

Hallo, i need a Prompting read in my script: read -p "Enter your command: " command But i always get this Error: -p: is not an identifier When I run these in c-shell i get this error /usr/bin/read: read: bad option(s) How can I use a Prompt in the read command? (9 Replies)
Discussion started by: wiseguy
9 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. UNIX for Dummies Questions & Answers

Convert "text" to "packed-decimal"?

Is there a way with HP-UX Release 10.20 (but going to HP-UX 11) to convert a regular "text" file to a packed data format (such as is created by a Cobol program)? (2 Replies)
Discussion started by: HuskyJim
2 Replies

Featured Tech Videos