using grep as if condition inside awk

12,384

Solution 1

You can use awk's system function:

cat /var/log/somelogfile | awk '{ if (system("grep -Fxq " $1 " textfile")) print "useful command " $1; }'

See the docs.

Solution 2

It LOOKS like what you're trying to do is:

awk '
NR==FNR { strings[$0]; next }
{
    for (string in strings) {
        if ( index($0,string) ) {
            print "useful command", $1
            next
        }
    }
}
' textfile /var/log/somelogfile

We'll know for sure if/when you post some some sample input/output.

Share:
12,384
user1739455
Author by

user1739455

Updated on June 04, 2022

Comments

  • user1739455
    user1739455 over 1 year

    I can use grep -Fxq search-string text-file outside of my awk, and it works they way I'd expect (found here How to test if string exists in file with Bash shell?). But, when I try to use that same grep command as an if statement inside awk, it seems to do nothing. Here's the basic usage I'm trying:

    cat /var/log/somelogfile | awk '{ if (grep -Fxq $1 textfile) print "useful command $1" }'
    
  • Jotne
    Jotne over 9 years
    You should not use cat with program that can read data itself, like awk, sed, perl etc.
  • Jotne
    Jotne over 9 years
    You should not use cat with program that can read data itself, like awk, sed, perl etc.
  • Jakub M.
    Jakub M. over 9 years
    True, modified the command. Nevertheless, I commit this dreadful sin of cating files and then grepping them since many years and I see it's drawbacks maybe once per year when I grep something really big. Otherwise you have nice data flow, with data source at left and filters one by one at right. Readability over performance.
  • Ed Morton
    Ed Morton over 9 years
    Nor do you need grep when you are using awk.
  • Ed Morton
    Ed Morton over 9 years
    That's testing for cases where a whole line matches a regexp which is completely different from testing for the existence of a string in a file as the OP is trying do do. The -F tells grep to look for strings instead of REs, in awk you use index() and == for that instead of match(), //, and ~.
  • djhaskin987
    djhaskin987 over 9 years
    It looks like the grep may be important to what the user is trying to do. I concede the cat point, though.
  • user1739455
    user1739455 over 9 years
    The grep was needed to check if a username (found in column 1 by awk) existed in the text file. I am parsing the cups page log to count the number of pages printed by each user and warn them if they exceed a certain limit. I wanted to run the script once a day, but not email the same user more than once a month, hence why I needed to check if they had already been warned ie if their username had already been added to the text file.
  • djhaskin987
    djhaskin987 over 9 years
    Grep is a system command, not an awk command. The stuff between the single quotes are bon a fied awk language, and grep is not part of that language. So you have to rely on the actual grep program and you call programs in awk with 'system()'. Again, see the docs.
  • Codoscope
    Codoscope about 6 years
    Very useful answer. In my case, I had to do if(system("grep " $1 " textfile > /dev/null") == "0") print ....