Search code examples
bashsearchawklines

Find specific lines in a file with bash


I have the following problem. I have files with thousands of lines (and around 1000 columns) and I only need some specific lines. In the second column of these files there is a indentifier which is unique for every line. For example:

0 tg573754 0 3455 B H G J
0 tg238576 0 4568 K L E S
0 tg289476 0 3246 L E S D

Let's assume there are thousands of lines and I want to extract those 3 lines out of the file. Till now I used:

awk '$2 == "tg573754"'
awk '$2 == "tg238576"'
awk '$2 == "tg289476"'

Is there a way to combine multiple searching requests in one row or to tell awk that it should refer to a separate file where all the unique identifier included which I need? Probably the last idea is the most elegant way but I don't know whether it is possible.

Thanks in advance for helping me.

Best, Tobi


Solution

  • You could use regex.

    awk '$2 ~ /^(tg573754|tg238576|tg289476)$/' file
    

    OR

    awk '$2 ~ /^tg(573754|238576|289476)$/' fi;e