use awk to extract row records contain specific word

awk, shell

Solution

Given your lines in file `data.txt`:

awk -F"\t" '/hello/ {print $0}' data.txt

will print

here is a hello       whateverColumn2
here hello again      mockColumn2

The `-F"\t"` sets tab as the field separator for the input lines.

Update (based on request in comments below by OP):

To make this case-insensitive use the `IGNORECASE` flag:

awk -F"\t" 'BEGIN{IGNORECASE=1} /hello/ {print $0}' data.txt

Note that the IGNORECASE variable is a GNU extension and may not be available in other versions of AWK.

Alternatively, an example using `match`. In order to make this case-insensitive, the input is converted into lower case:

awk -F"\t" '{if (match(tolower($0), "hello")) print $0}' data.txt

Since match can take regular expressions, the conversion to lowercase may not be necessary with the right regular expression.

Tested with GNU Awk 3.1.6 under Linux

Problem

Say my input file is tab delimited, how do i identify if $0 contains a word "hello" and it needs to be case insensitive? ``` here is a hello whateverColumn2 nonono nonono whateverItIs here HeLLo again mockColumn2 ``` Thanks a lot!

Original source