Search code examples
unixwc

Counting lines with specific criteria unix


I have a database with 7 columns (file.txt). I have a list with names (names.txt). I want to count the lines in file.txt where a name from names.txt appears both in column 3 and 4. Said in another way, I don't want to count the lines where the name appears only in one column of file.txt or it doesn't appear at all. How can I do that in unix? Thanks.


Solution

  • awk -F, 'BEGIN {
        while ((getline name < "names.txt") > 0) {
            names[name] = 1
        }
        close("names.txt")
        count = 0
    }
    $3 in names && $4 in names { count++ }
    END { print count }' file.txt