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.
Author by
user1739455
Updated on June 04, 2022Comments
-
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 over 9 yearsYou should not use
cat
with program that can read data itself, likeawk
,sed
,perl
etc. -
Jotne over 9 yearsYou should not use
cat
with program that can read data itself, likeawk
,sed
,perl
etc. -
Jakub M. over 9 yearsTrue, modified the command. Nevertheless, I commit this dreadful sin of
cat
ing files and thengrep
ping 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 over 9 yearsNor do you need grep when you are using awk.
-
Ed Morton over 9 yearsThat'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 useindex()
and==
for that instead ofmatch()
,//
, and~
. -
djhaskin987 over 9 yearsIt looks like the
grep
may be important to what the user is trying to do. I concede thecat
point, though. -
user1739455 over 9 yearsThe 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 over 9 yearsGrep 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 about 6 yearsVery useful answer. In my case, I had to do
if(system("grep " $1 " textfile > /dev/null") == "0") print ...
.