use backtick inside awk

Hello,

Can't we use backtick operator inside awk.

nawk '
  BEGIN {
    RS=""; FS="\</input\>"
  }
  {
    for(i=1;i<=NF;i++) {
      if ($i~/\"\"/) {
        print `grep XYZ $i`;
        print $i
      }
    }
  }
' test

In the following code, I need to print $i and some values from $i only when the 'if' condition satisfies. But it seems back tick operator doesn't work inside awk.
Please tell me if I am doing something wrong or a workaround for it.

Thanks to all.

awk has system() instead.

Thanks Corona688.... But my requirement is a little different.. I am sorry for not explaining the problem statement well in my previous post.

 nawk '
  BEGIN {
    RS=""; FS="\</input\>"
  }
  {
    for(i=1;i<=NF;i++) {
      if ($i~/\"\"/) {
        print `grep XYZ $i`;
        print $i
      }
    }
  }
' test

I have an xml file having multiple <input> , </input> tags. All lines between one <input> , </input> is one order. I need all orders from xml where value of any attribute is "". So I used </input> as field separator and printed all the fields (i.e. orders) where value="".
That's what I am doing with if statement.

Now I need two attributes from all orders which I am printing. So I want to do a grep and print on $i. That's I am unable to do.
using system("grep XYZ $i") enters into infinite loop and using system(grep XYZ $i) gives me value 2.

Hi,

It's probably awk can do the job without calling to external program, or pipe the result to a 'grep' process instead of using it inside.

Post an example of your input file.

Regards,
Birei

system("grep XYZ $i") 

This will have issues because the $i is in quotes and thus awk will put a literal $i in the command rather than what is in column (field) i.

 system(grep XYZ $i)
    

This will also have issues because awk will attempt to use 'grep,' and 'XYZ' as variable names -- they probably aren't assigned and thus evaluate to blanks. The contents of field i will be put in as the command, so who knows what will actually be executed (lets hope the second field doesn't contain rm * ).

What you want is a combination:

system( "grep XYZ " $i );

Notice the space after the Z. If you don't put it there you'll likely get an error from grep, or other odd results.

I still am not convinced that your grep command is correct as I don't get the feeling that $(i) contains a filename to search. Maybe it does, but it seems unlikely from what you've posted. As birei suggested, post a sample of your XML and there's a good chance that you'll not need system() at all.

Thanks.

I actually need to find if a string (a) is present in an external file (error.log)? The string (a) is calculated from nawk loop.
The code:

nawk '
  BEGIN {
    RS=""; FS="\</input\>"
  }
  {
    for(i=1;i<NF;i++) {
      n=split($i,arr," ");
      a=arr[4] # This is: 14:14:06
      # check if 'a' is present in "error.log", if not, print $i
      <missing code>
    }
  }
' tmp

That's why I need grep inside nawk. Plz tell me if there's other ways to do this.
I simply need to check if a string (calculated in nawk) is present in an external file.. This has to be done for several times, so I put that inside a loop.

Thanks.

You don't actually need the output text from grep, hence don't actually need backticks.

r=system("grep -q " something " " filename);
if(r == 0)
{
# something in file
}
else
{
# something not in file
}

@Corona688

Its giving me an error:

grep: illegal option -- q
Usage: grep -hblcnsviw pattern file . . .

I am using Solaris.

then just tack >/dev/null onto the end of the string so it doesn't spew output to awk's stdout.