grep 模式与文件完全匹配并仅在第一列中搜索

Fra*_*pis 4 command-line shell grep regular-expression

我有一个这样的大文件:

denovo1 xxx yyyy oggugu ddddd
denovo11 ggg hhhh bbbb gggg
denovo22 hhhh yyyy kkkk iiii
denovo2 yyyyy rrrr fffff jjjj
denovo33 hhh yyy eeeee fffff
Run Code Online (Sandbox Code Playgroud)

那么我的模式文件是:

denovo1
denovo3
denovo22
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用fgrep以便仅提取与我的文件中的模式完全匹配的行(所以我想要denovo1但不是denovo11)。我试图-x用于精确匹配,但后来我得到了一个空文件。我试过:

fgrep -x --file="pattern" bigfile.txt > clusters.blast.uniq
Run Code Online (Sandbox Code Playgroud)

有没有办法只在第一列中进行 grep 搜索?

ste*_*ver 6

你可能想要这个-w标志——来自man grep

   -w, --word-regexp
          Select  only  those  lines  containing  matches  that form whole
          words.  The test is that the matching substring must  either  be
          at  the  beginning  of  the  line,  or  preceded  by  a non-word
          constituent character.  Similarly, it must be either at the  end
          of  the  line  or  followed by a non-word constituent character.
          Word-constituent  characters  are  letters,  digits,   and   the
          underscore.
Run Code Online (Sandbox Code Playgroud)

IE

grep -wFf patfile file
denovo1 xxx yyyy oggugu ddddd
denovo22 hhhh yyyy kkkk iiii
Run Code Online (Sandbox Code Playgroud)

要仅在第一列中强制匹配,您需要修改模式文件中的条目以添加行锚点:您还可以使用\b单词锚点而不是命令行-w开关,例如在patfile

^denovo1\b
^denovo3\b
^denovo22\b
Run Code Online (Sandbox Code Playgroud)

然后

grep -f patfile file
denovo1 xxx yyyy oggugu ddddd
denovo22 hhhh yyyy kkkk iiii
Run Code Online (Sandbox Code Playgroud)

请注意,-F如果文件包含正则表达式而不是简单的固定字符串,则必须删除开关。