Evaluate Expression within awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Evaluate Expression within awk
# 1  
Old 09-12-2014
Evaluate Expression within awk

I want to create a conditional expression string and pass in an awk script. My script is as below...

Code:
comm="\$3 == "hello"" 

awk -F "^T" -v command="${comm}" ' {

     if ( command )   {     print "hye"   } 
}' test

But the statement "if ( command )" always evaluates to true which is not correct. I want to know the correct way of executing this statement..
On Running...

Code:
./test_awk.sh
hye ( not correct )

Test file has following ( just a sample )

Code:
hello^Thye^Tbye

Please help....
# 2  
Old 09-12-2014
The way you're doing it, command is a string; not an awk condition to be processed. Since the string $3 == hello is not an empty string and does not evaluate to "0", testing the string yields TRUE. The following should work but it gets a lot more complex if the operator you want varies from invocation to invocation rather than being a constant ==

Code:
field=3
value="hello" 

awk -F "^T" -v fn="$field" -v val="$value" '
{     if ( $fn == val )   {     print "hye"   } 
}' test

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-12-2014
Thanks and need more help

Quote:
Originally Posted by Don Cragun
The way you're doing it, command is a string; not an awk condition to be processed. Since the string $3 == hello is not an empty string and does not evaluate to "0", testing the string yields TRUE. The following should work but it gets a lot more complex if the operator you want varies from invocation to invocation rather than being a constant ==

Code:
field=3
value="hello" 

awk -F "^T" -v fn="$field" -v val="$value" '
{     if ( $fn == val )   {     print "hye"   } 
}' test

Thanks Sir for the reply.. But I want to formulate a complex string command to be passed to the awk function.. For example, I want to have an expression like this

Code:
str="($3 > 0 && $1 == 10) || ($2 == 100)"

Then I want it to be evaluated in awk like

Code:
awk -v command=${str} '{
if ( $command )
    print success
fi }' test

How can I achieve this kind of functionality

Many thanks in advance

Last edited by vbe; 09-12-2014 at 06:20 AM.. Reason: insert between the code tags ! Thanks
# 4  
Old 09-12-2014
Try putting your complex condition into a file and run awk with the -f option: awk -f file
# 5  
Old 09-12-2014
But what is the logic to be used in this case??
# 6  
Old 09-12-2014
Try
Code:
echo '{if (($3 > 0 && $1 == 10) || ($2 == 100)) print "success"}' > awkscript
awk -f awkscript file4

You can have multiple script files on the command line.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

How to evaluate a variable name on LHS of expression?

I am trying to write a simple function to select values from a database and assign them to variables. It can have any number of arguments sent into it, and I want to assign the value retrieved to a different variable name for each argument sent in. So my code looks something like this: ... (6 Replies)
Discussion started by: DJR
6 Replies

3. Shell Programming and Scripting

awk print expression

Hi All, I have a doubt in awk print exp. Where in some awk commands have seen a digit 1 appended at the end of the awk ,didnt remember the command . like .. cat file |awk '{print }1' Could some one help in understanding these cases where we use them. Regards, Ganesh, (2 Replies)
Discussion started by: rmkganesh
2 Replies

4. Shell Programming and Scripting

awk regular expression

Hello, I have big files which I wanna filter them based on first column. first column should be one of these strings: chr2L || chr2R || chr3L || chr3R || chr4 || chrX and something like chr2Lh or chrY or chrM3L is not accepted. I used the following command: awk '{ if ($1=="chr2L" ||... (5 Replies)
Discussion started by: @man
5 Replies

5. Shell Programming and Scripting

Help in understanding awk expression

Hi, Could somebody help me in understanding the following awk expression: awk -v n="POINT" '/%/{print $0 "\n" n ;next}1' < file name Thanks, Arun (6 Replies)
Discussion started by: arun_maffy
6 Replies

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

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. UNIX for Dummies Questions & Answers

Awk inside Awk expression

Hi, It can be used awk inside other Awk?. I need to get another text processing while other text process. Thank you. (2 Replies)
Discussion started by: pepeli30
2 Replies

9. Shell Programming and Scripting

Regular expression in AWK

Hello world, I was wondering if there is a nicer way to write the following code (in AWK): awk ' FNR==NR&&$1~/^m$/{tok1=1} FNR==NR&&$1~/^m10$/{tok1=1} ' my_file In fact, it looks for m2, m4, m6, m8 and m10 and then return a positive flag. The problem is how to define 10 thanks... (3 Replies)
Discussion started by: jolecanard
3 Replies

10. Shell Programming and Scripting

Check for more than one expression using AWK

I have a txt file like below: testin.txt AB BC CD DE I have the following awk script BEGIN {flag1="N"} /(AB)|(BC)|(CD)|(DE)/ {flag1="Y"} END {print flag1} >awk -f testin.awk testin.txt Returns Y (8 Replies)
Discussion started by: visakhcr
8 Replies
Login or Register to Ask a Question