String compare using awk - what am I doing wrong?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String compare using awk - what am I doing wrong?
# 1  
Old 10-26-2013
Network String compare using awk - what am I doing wrong?

Hi all,
I was doing some string manipulation in my script and wanted to try using awk. However, I have been stuck with string compare. A simplified form of my conundrum is given below:

The below prints expected result (prints "Completed because that is the second element"):
Code:
$ echo '"status":"Completed"' | awk 'BEGIN{FS=":"} /"status"/ {print $2}'
"Completed"

The below does not print the expected value:
Code:
$ echo '"status":"Completed"' | awk 'BEGIN{FS=":"} /"status"/ {print ($2 == "Completed") ? "Yes" : "No"}'
No

Shouldn't the output above be "Yes" given $2 is "Completed"? What am I doing wrong?

Btw, what I really need to do is set a variable if the string compare is successful. The print-test was just to see if it's work at all, and looks like I need to rethink.

Any help is highly appreciated.

I am a newbie to this forum and apologize if I'm creating an unnecessary thread (as opposed to logging this in an existing thread). If there is one that answers my question, please do suggest.
# 2  
Old 10-26-2013
Hi you should be testing for "\"Completed\""
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 10-27-2013
Or ($2 ~ /Completed/)?
This User Gave Thanks to RudiC For This Post:
# 4  
Old 10-27-2013
Scrutinizer and RudyC's approach

Code:
$ echo '"status":"Completed"' | awk -F'[:]' '{print ($2 ~ /Completed/)? "Yes": "No"}'
Yes
$ echo '"status":"Completed"' | awk -F'[:]' '{print ($2 == "\"Completed\"")? "Yes": "No"}'
Yes

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 10-28-2013
You could use grep -q:

Code:
echo '"status":"Completed"' | if grep -q '"status":"Completed"'
then
    echo Yes
else
    echo No
fi

# 6  
Old 10-28-2013
Thank you all for the timely and useful help. Highly appreciated.

Scrutinizer, RudiC,
Your suggestions were both apt. Thanks.

Akshay,
I will need to read on your usage of
Code:
-F'[:]'

I am just beginning to scratch the surface of awk and there are a ton of things I do not yet know. Thanks for the new (for me) usage.
Btw, could you please elaborate a bit on what that expression does? Can I also use something similar for rows (i.e. RS)?

Chbler,
The reason I landed on awk is that I need to read a Json file, and it is essential I break it down into records and fields so I could process it in a structured manner. Thanks for your time nonetheless.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. Shell Programming and Scripting

Pass column number as variable to awk and compare with a string.

Hi All, I have a file test.txt. Content of test.txt : 1 vinay se 2 kumar sse 4 kishore tl I am extracting the content of file with below command. awk '$2 ~ "vinay" {print $0}' test.txt Now instead of hardcoding $2 is there any way pass $2 as variable and compare with a... (7 Replies)
Discussion started by: Girish19
7 Replies

3. Shell Programming and Scripting

What's wrong with my awk

file1: maximum_delay time: 102.794 ms maximum_delay time: 92.977 ms maximum_delay time: 98.895 ms maximum_delay time: 96.891 ms maximum_delay time: 86.966 ms maximum_delay time: 95.91 ms maximum_delay time: 98.921 ms maximum_delay time: 89.881 ms maximum_delay time: 92.931 ms... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

4. Shell Programming and Scripting

awk - how to compare part of the string?

Need help for awk.. file will have comma separated numbers, I need check digits before 10 numbers eg ( 001)1234567890 Basically want to check country code of a mobile number. eg: abc,def,data, data,0011234567890, data,data Script should be checking country code with 001, I will pass... (10 Replies)
Discussion started by: vegasluxor
10 Replies

5. Solaris

Korn shell - evaluating string gives wrong results due to limitations

Anyone ever seen this? Someone mentioned this the other day.... If you do, for instance, in korn shell, echo $(5.2+2.5), it gives the result of 6 regardless. Can't remember why but it was the limitation of the korn shell. (5 Replies)
Discussion started by: psychocandy
5 Replies

6. Shell Programming and Scripting

What's wrong with this awk?

I have a file and if I'm doing following action on that file it's coming up correctly awk -F"," '/DATA_TYPE/ { cnt += $3 } END { print " DATA_TYPE count=" cnt}' inter DATA_TYPE count=19593131 now if I'm changing same AWK, so that now it can accept variable, then it's somehow not working.... (3 Replies)
Discussion started by: manas_ranjan
3 Replies

7. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

8. Shell Programming and Scripting

awk if statement to evaluate string and compare

I have the following simplified code that I am planning on putting into a larger shell script. I have been butchering it to try and make work amongst google searches and reading awk documentation. amixer sset Master toggle | awk '{ if ( /^ Front Left/ { print $7 } == // ) print "MUTED" }'I... (2 Replies)
Discussion started by: jelloir
2 Replies

9. Shell Programming and Scripting

[awk]compare a number in a string with a list

Hi, I have a program written in awk and I want to extend it to do another task. My program is a list of CVS log reports of a repository. For each file, I have some fields. One of the fields is the comment field. I want to know how I can check if a comment (which is a free text field)... (8 Replies)
Discussion started by: sandeepk1611
8 Replies

10. Shell Programming and Scripting

awk... what is wrong?

Hi there, what is wrong with this? grep ">>" alarms_temp | awk '{print substr($5, 2), $7}' | read var1 var2 echo "$var1" echo "$var2" Both variable are empty, but if i run: grep ">>" alarms_temp | awk '{print substr($5, 2), $7}' I have: 0 9 as a result. (2 Replies)
Discussion started by: marimovo
2 Replies
Login or Register to Ask a Question