0

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

Ghost
  • 1,426
  • 5
  • 19
  • 38
  • Possible duplicate of [How to test if string exists in file with Bash shell?](http://stackoverflow.com/questions/4749330/how-to-test-if-string-exists-in-file-with-bash-shell) – Julien Lopez Oct 10 '16 at 11:18
  • Actually, something like that is what i'm currently using as a workaround (outputting to a file between and then processing), but i was wondering if there was a way to do it directly – Ghost Oct 10 '16 at 11:50
  • You can use command substitution: `grep -Fx $(anything) file`. See https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html – Julien Lopez Oct 10 '16 at 11:57

2 Answers2

1

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
jas
  • 10,715
  • 2
  • 30
  • 41
  • Excellent! Didn't know about -, thanks a lot! Another question.. how can i "manipulate" the output of each input? Let's say i want to get the first column in the stdin ($1), but the second ($2) in the file. – Ghost Oct 10 '16 at 11:42
  • Perfect bro, that's what i was looking for, thanks a lot! – Ghost Oct 10 '16 at 11:59
0
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.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185