I found myself having to learn a bit of awk, and it's really cool, but I have a small problem I am facing.

I have a script that has an output that is designed to be piped. Let's call it matrix.

I also have a local text file blacklist.txt over which I have complete control and can format any way I want. It only contains 1 column (or one set of values)

I want to run the script, pipe it, compare one of the resulting column values with each line (or comma separated) value in the text file, and if it's a match executing something else.

I want to run the following (with some pseudo code):

matrix username |    // this returns an output and is piped
// I then want to awk in a blacklist of usernames from my file,
//not sure if I should use getline
awk '{print $1}' blacklist.txt
// And now, I would like to compare the two, and execute another command
if($4 from matrix == $1 from blacklist.txt) {
  matrix doSomething
else {
  print "Not valid"

Any pointers would be greatly appreciated. Thank you.

  • So, you want to execute matrix doSomething for each line that matches?
    – muru
    Nov 17, 2015 at 3:40
  • Must it be awk or can you use | while read -r one to three four others; do something_with_four ?
    – Walter A
    Nov 17, 2015 at 22:29

1 Answer 1


This can be acomplished using two features of awk:

  1. arrays are indexed by strings
  2. the built-in variable FILENAME

This script assumes default field separators, and that blacklist.txt contains one field per line. Adjust according to your needs.


awk '
    FILENAME == "blacklist.txt" {
        blacklist[$0] = 1
    blacklist[$4] {
        print $4, $0
' blacklist.txt -

Not the answer you're looking for? Browse other questions tagged .