awk if statement to evaluate string and compare


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk if statement to evaluate string and compare
# 1  
Old 12-06-2011
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.

Code:
amixer sset Master toggle | awk '{ if ( /^  Front Left/ { print $7 } == /[off]/ ) print "MUTED" }'

I want to evaluate the output from the syntax below and compare it with the value "[off]" and print the word "MUTED" if it matches.

Code:
amixer sset Master toggle | awk '/^  Front Left/ { print $7 } '

The command essentially outputs either [on] or [off]

I have been unable to find an example and I'm not even sure it could be done - Would an awk guru please provide some insight.

The input to awk from the amixer command looks like this for anyone that does not have amixer available and would like to test using a text file (the [off] becomes [on] and visa versa each time the command is executed).

Code:
Simple mixer control 'Master',0
  Capabilities: pvolume pswitch pswitch-joined penum
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 74
  Mono:
  Front Left: Playback 50 [68%] [-24.00dB] [off]
  Front Right: Playback 50 [68%] [-24.00dB] [off]

Thanks
# 2  
Old 12-06-2011
Try this...
Code:
amixer... | awk '/^  Front Left/{if($7~"[off]"){print "MUTED"}}'

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 12-06-2011
MySQL

You Rule!! Thanks
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. UNIX for Advanced & Expert Users

How to evaluate expression under awk?

I have to display only those subscribers which are in "unconnected state" and the date is 90 days older than today's date. Below command is used for this purpose: cat vfsubscriber_20170817.csv | sed -e 's/^"//' -e '1d' | \ nawk -F '",' '{if ( (substr($11,2,4) == 2017) && ( substr($11,2,8)... (1 Reply)
Discussion started by: dia
1 Replies

3. Shell Programming and Scripting

Evaluate Expression within awk

I want to create a conditional expression string and pass in an awk script. My script is as below... comm="\$3 == "hello"" awk -F "^T" -v command="${comm}" ' { if ( command ) { print "hye" } }' testBut the statement "if ( command )" always evaluates to true which is not... (5 Replies)
Discussion started by: Saikat123
5 Replies

4. Shell Programming and Scripting

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"): $ echo... (5 Replies)
Discussion started by: faraway
5 Replies

5. 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

6. Shell Programming and Scripting

How to Evaluate two conditions in single if statement

I am trying to test two conditions in a single if and getting syntax error on -a and && if ] ; then echo "variable a equals to variable b" else echo "variable a not equal to variable b" fi in second attempt I used -a instead of &&, referring to other website, but not sure that... (1 Reply)
Discussion started by: praxis1
1 Replies

7. Shell Programming and Scripting

How to evaluate a string of numbers in the same command of AWK

Hi, I am trying to do evaluate one numerical string after substitution. ++++++++++++++++== What I have = "7.04+2.3Xlog(0.72e-6X1.0e6)X1.9596" What I need = evaluate 7.04+2.3*log(0.72e-6*1.0e6)*1.9596 = 5.55941 what I am doing; echo "7.04+2.3Xlog(0.72e-6X1.0e6)X1.9596" | awk... (2 Replies)
Discussion started by: vivek_shm74
2 Replies

8. 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

9. Shell Programming and Scripting

Evaluate string containing shell variable names

Hello, I have this: #!/usr/bin/ksh V1=ABC str="hello 123;${V1}" eval "echo $str" i get hello 123 /script.sh ABC not found However eval works if $str variable doesn't contain a semicolumn (eg if str="hello 123~${v1}" running the eval statement above would produce (2 Replies)
Discussion started by: endorphin
2 Replies

10. Shell Programming and Scripting

Very urgent :- How to compare string using if statement

Hi How can i compare a string using if statement in a script? For eg: I have filename="abc.sh" if ;then { ....... ....... } fi but this doesnot work .. How will i execute this ?? Thanks in advance (1 Reply)
Discussion started by: jisha
1 Replies
Login or Register to Ask a Question