Skip to content
Advertisement

Filtering on a condition using the column names and not numbers

I am trying to filter a text file with columns based on two conditions. Due to the size of the file, I cannot use the column numbers (as there are thousands and are unnumbered) but need to use the column names. I have searched and tried to come up with multiple ways to do this but nothing is returned to the command line.

Here are a few things I have tried:

awk '($colname1==2 && $colname2==1) { count++ } END { print count }' file.txt to filter out the columns based on their conditions

and head -1 file.txt | tr 't' | cat -n | grep "COLNAME to try and return the possible column number related to the column.

An example file would be:

JavaScript

Output would be: 2 (count of ad=c and bd=water)

Advertisement

Answer

with your input file and the implied conditions this should work

JavaScript

or you can replace c1 and c2 with the values in the script as well.

to find the column indices you can run

JavaScript

or perhaps with this chain

JavaScript

although may have false positives due to regex match…

You can rewrite the awk to be more succinct

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement