Skip to content
Advertisement

Regular expression to search column in text file

I am having trouble getting a regular expression that will search for an input term in the specified column. If the term is found in that column, then it needs to output that whole line. These are my variables:

JavaScript

the text file is in this format with a space being the field seperator, with many contact entries:

JavaScript

I’ve tried with no success:

JavaScript

Thanks for any help!

Advertisement

Answer

awk may be the best solution for this:

JavaScript

This checks if the field number $field has the value $name. If so, awk automatically prints the full line that contains it.

For example:

JavaScript

As you can see, we give the parameters using -v var="$bash_var", so that you can use them inside awk.

Also, the space is the field separator, so you don’t need to specify it since it is the default.

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