bash:基于第一列的grep完全匹配

Lis*_*ann 3 linux shell command-line grep

我有一个.txt文件,如下所示:

9342432_A1 9342432 1 0 0 0
4392483_A2 4392483 2 0 0 0 
4324321_A3 4324321 1 0 0 0
9342432    9342432 2 0 0 0
Run Code Online (Sandbox Code Playgroud)

例如,我想生成ID为4324321_A3和9342432的子集(基于第一列!).我尝试了以下命令来查找完全匹配:

 grep -E '4324321_A3|9342432'
Run Code Online (Sandbox Code Playgroud)

但是当我使用这一行时,我最终会得到一个这样的数据集:

9342432_A1 9342432 1 0 0 0
4324321_A3 4324321 1 0 0 0
9342432    9342432 2 0 0 0
Run Code Online (Sandbox Code Playgroud)

问题是匹配部分ID(9342432_A1)的行不应该存在.谁能帮我这个?

我想最终得到这个:

4324321_A3 4324321 1 0 0 0
9342432    9342432 2 0 0 0
Run Code Online (Sandbox Code Playgroud)

fed*_*qui 7

它匹配

9342432_A1 9342432 1 0 0 0
Run Code Online (Sandbox Code Playgroud)

因为它9342432在第二列中.

您需要更新命令以使grep检查行以这些单词开头,即使用^word:

$ grep -E '^4324321_A3|^9342432' file
4324321_A3 4324321 1 0 0 0
9342432    9342432 2 0 0 0
Run Code Online (Sandbox Code Playgroud)

为了使其更准确,您还可以使用-w匹配完整单词.这种方式grep -wE '^4324321_A3|^9342432' file不匹配一条线

4324321_A3something 4324321 1 0 0 0
Run Code Online (Sandbox Code Playgroud)