Linux bash script: how to search by column but return full string?

I have a tab delimited file with data like this:

col1    col2    col3

      

I wrote a bash script that allows you to search for a file using this code:

echo -en "Search term: "
read search

data=`cat data.data | egrep -i "$search"`

      

This works great for searching the entire file, but now I only want to search on a specific column (which the user can select).

I know the command cut

and can search on a column using this:

cat data.data | cut -f$col | egrep -i "$search"

      

But then only that column is displayed, so if I use this method, I need to somehow return the rest of the row.

How can I search by column in a file, but return full rows for the results?

+3


source to share


2 answers


awk is easier for this:

data=$(awk -v col=$col -v term="$term" 'toupper($col)==toupper(term)' file)

      



col - column number
term - search query

You can also pass in a field separator -F

if needed.

+1


source


You can pass two variables to awk: the column number and the search term.

awk -vcol="$col" -vsearch="$search" '$col ~ search' data.data

      

If the value $col

is 2, then $2

awk will match the second column. The operator is ~

used to match a regular expression pattern. The string will be printed if the column matches the regular expression.

Testing:



$ 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 make the pattern match case sensitive, you have two options: convert everything to upper or lower case ( tolower($col) ~ tolower(search)

), or if you're using GNU awk, set the IGNORECASE variable:

$ search=L2
$ awk -vIGNORECASE=1 -vcol="$col" -vsearch="$search" '$col ~ search' data.data
col1    col2    col3

      

+3


source







All Articles