1

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"
  }

  if ( $3 == "hello" )
  {
      print "bye"
  }
}' 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...

./test_awk.sh

It returns

hye

Which is not the expected answer.

Test file has following ( just a sample ):

hello^Thye^Tbye

Many thanks in advance

  • You want to use the pipe notation and getline for getting output from external commands, see this manual page for example. BUT depending on what you are trying to do, which is not clear from the example, this might not be the way to go. – Thor Sep 12 '14 at 6:51
  • I am trying to evaluate the conditional expression within awk.. My expression is "\$3 == hello" and the modified script is comm="\$3 == "hello"" awk -F "^T" -v command="${comm}" '{ if ( command ) { print "hye" } if ( $3 == "hello" ) { print "bye" } }' test The file considered is test. If the 3rd field in file test is hello, the conditional expression shud evaluate to true. which is being passed in variable "comm" above.. Please help – Saikat Dasgupta Sep 12 '14 at 6:56
  • Please add this information together with a sample test file to the question by using the edit function, – Thor Sep 12 '14 at 7:05
  • Could you post awk --version? – Lety Sep 12 '14 at 8:02
  • 1
    I suggest you rethink your approach - for example using a here-document so that the shell expands your variable expression before passing it to awk. – steeldriver Sep 12 '14 at 10:13
2

If the logical operation is always the same (i.e. in this case ==) and you just want to change the field number and the value against which it is tested, then you can pass them as separate variables e.g.

#!/bin/bash

awk -v field=3 -v value="hello" '
{
  if ( $field == value )
  {
    print "hye"
  }

  if ( $3 == "hello" )
  {
      print "bye"
  }
}' test

If you truly want to create a variable awk script, then IMHO a different approach is required, for example using a here document to write the awk script on the fly using the shell to expand your command variable - something like

#!/bin/bash

mycommand="\$3 == \"hello\""

awk -f- test << EOF 
{
  if ( ${mycommand} )
  {
    print "hye"
  }

  if ( \$3 == "hello" )
  {
      print "bye"
  }
}
EOF
  • Probably this is the right approach, awk consider value in -v var=value as string and do not parse "$3 ==". According to manual if (expr) return true if expr is a string not empty. – Lety Sep 12 '14 at 20:45
0

Lets trace:

$ cat test
hello^Thye^Tbye

$ awk  '{ print ($3 == hello) ? "true" : "false" }' test
true # wrong output

$ awk  '{ print ($3 == "hello") ? "true" : "false" }' test
false # correct output

according to trace when we use $3 == hello we get wrong output and when changed to $3 == "hello" including with double quote around the string the output is correct.

So we found the wrong statement and the problem is on comm variable value if you set:

$ comm="\$3 == "hello""

and take echo:

$ echo $comm

you will see the comm variable has below value in it

$3 == hello

so you need double quote around the value then edit your value with this one:

$ comm="\$3 == \"hello\""
$ echo $comm
$3 == "hello"

then go and improve your's. hope this helps.

  • @KaisyA, did you test your suggestion? Problem is related to "$3" that is not evaluated inside awk, isn't it? – Lety Sep 12 '14 at 10:39
  • @Letizia I traceed that for myself and when I forget double quote I get wrong result. – αғsнιη Sep 12 '14 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.