use backtick inside awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting use backtick inside awk
# 1  
Old 08-12-2011
use backtick inside awk

Hello,

Can't we use backtick operator inside awk.
Code:
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.
# 2  
Old 08-12-2011
awk has system() instead.
# 3  
Old 08-12-2011
Thanks Corona688.... But my requirement is a little different.. I am sorry for not explaining the problem statement well in my previous post.


Code:
 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.
# 4  
Old 08-12-2011
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
# 5  
Old 08-12-2011
Code:
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.


Code:
 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:
Code:
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.

Last edited by agama; 08-12-2011 at 08:46 PM.. Reason: typo
# 6  
Old 08-15-2011
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:
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.
# 7  
Old 08-15-2011
You don't actually need the output text from grep, hence don't actually need backticks.
Code:
r=system("grep -q " something " " filename);
if(r == 0)
{
# something in file
}
else
{
# something not in file
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

2. Shell Programming and Scripting

[Solved] Backtick and escapes

Hello all, I have a problem with a bash script. It contains an MySQL query, which when I run it 'as is', executes without a problem. When, however, I try to get it to assign its output to a variable, using the backtick, I get errors. So .. /usr/bin/mysql -N -B mydatabase -e 'SELECT... (3 Replies)
Discussion started by: davidm123SED
3 Replies

3. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

4. Shell Programming and Scripting

Perl: Backtick Errors

When trying to use backticks for system commands, is there a way to read the error messages if a command doesn't execute properly? I have no problem getting the results if the command is properly executed. Ex. my @result = `dir`; foreach my $line (@result) { print "Result = $line";... (2 Replies)
Discussion started by: kooshi
2 Replies

5. Shell Programming and Scripting

Perl: combine Backtick & system() I/O operation?

Hi - Within perl I want to execute a system command. I want to re-direct all the output from the command to a file (@result = `$cmd`;), but I ALSO want the results to be displayed on the screen (system("$cmd"); The reason is this - if the command completes, I want to process the output. If the... (6 Replies)
Discussion started by: jeffw_00
6 Replies

6. Shell Programming and Scripting

awk inside another awk statement

hi all, i have two files 1) a.txt one two three 2) abc "one" = 10 pqr "three" = 20 345 "two" = 0 this is what i want in third file (3 Replies)
Discussion started by: shishirkotkar
3 Replies

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

8. Shell Programming and Scripting

Awk problem: How to express the backtick(')

For example: I got a list of file end at .txt. I want all of them do the same command like grep '^@' and attached it to a output .sh file. This is the command I type: ls *.txt | awk '{print "grep \' \^\@\' ",$1}' > txt.sh My desired output is when I type the command "more txt.sh " The... (4 Replies)
Discussion started by: patrick87
4 Replies

9. Shell Programming and Scripting

By using awk, how to '(backtick)?

Can I know how to express the '(backtick) in awk?! By typing \' ??? (8 Replies)
Discussion started by: patrick87
8 Replies

10. UNIX for Advanced & Expert Users

Help with GNU screen: Backtick and Caption.

I'm trying to get GNU screen to show the output of "uptime" for the host being accessed in the current window, but unfortunately, no matter what window I go in, it shows the uptime for the host I originally launched screen in ("adminhost"). Does anyone know how to get this to update from the... (0 Replies)
Discussion started by: akbar
0 Replies
Login or Register to Ask a Question