Linux bash script: how to search on a column but return full row?
I have a tab-delimited file that contains data like :
col1 col2 col3
I wrote a bash script that allows searching for files using the following code:
echo -en "Search term: "
read search
data=`cat data.data | egrep -i "$search"`
This is great for searching the entire file, but I only want to search specific columns now (which the user can select).
I know the cut
command and can search for columns using this command:
cat data.data | cut -f$col | egrep -i "$search"
But only that one column is output, so if I use this method, then I need to somehow get back the rest of that row.
How do I search for a column in a file but return the full row of results?
Solution
You can pass two variables to awk: column number and search term.
awk -vcol="$col" -vsearch="$search" '$col ~ search' data.data
If the value of the $col
is 2, then $2
in awk corresponds to the second column. The ~
operator is used for regular expression pattern matching. If the column matches the regular expression, the row is printed.
Test:
$ cat data.data
col1 col2 col3
$ col=2
$ search=l2
$ awk -vcol="$col" -vsearch="$search" '$col ~ search' data.data
col1 col2 col3
$ search=l3
$ awk -vcol="$col" -vsearch="$search" '$col ~ search' data.data
# no output
If you want to do case-insensitive pattern matching, you have two options: convert everything to uppercase or lowercase (tolower($col) ~ tolower(search)
), or if you are using GNU awk, set the IGNORECASE variable:
$ search=L2
$ awk -vIGNORECASE=1 -vcol="$col" -vsearch="$search" '$col ~ search' data.data
col1 col2 col3