Reputation: 1523
I'm piping the output of a command to awk and i want to check if that output has a match in the lines of a file.
Let's say i have the following file:
aaa
bbb
ccc
...etc
Then, let's say i have a command 'anything' that returns, my goal is to pipe anything | awk
to check if the output of that command has a match inside the file (if it doesn't, i would like to append it to the file, but that's not difficult..). My problem is that i don't know how to read from both the command output and the file at the same time.
Any advice is welcome
Upvotes: 0
Views: 74
Reputation: 10865
My problem is that i don't know how to read from both the command output and the file at the same time.
Use -
to represent standard input in the list of files for awk to read:
$ cat file
aaa
bbb
ccc
$ echo xyz | awk '{print}' - file
xyz
aaa
bbb
ccc
EDIT
There are various options for handing each input source separately:
Using FILENAME
:
$ echo xyz | awk 'FILENAME=="-" {print "Command output: " $0} FILENAME=="input.txt" {print "from file: " $0}' - input.txt
Command output: xyz
from file: aaa
from file: bbb
from file: ccc
Using ARGIND
(gawk only):
$ echo xyz | awk 'ARGIND==1 {print "Command output: " $0} ARGIND==2 {print "from file: " $0}' - input.txt
Command output: xyz
from file: aaa
from file: bbb
from file: ccc
When there are only two files, it is common to see the NR==FNR
idiom. See subheading "Two-file processing" here: http://backreference.org/2010/02/10/idiomatic-awk/
$ echo xyz | awk 'FNR==NR {print "Command output: " $0; next} {print "from file: " $0}' - input.txt
Command output: xyz
from file: aaa
from file: bbb
from file: ccc
Upvotes: 1
Reputation: 203229
command | awk 'script' file -
The -
represents stdin. Swap the order of the arguments if appropriate. Read Effective Awk Programming, 4th Edition, by Arnold Robbins to learn how to use awk.
Upvotes: 0