grep パターンはファイルと正確に一致し、最初の列でのみ検索します。

grep パターンはファイルと正確に一致し、最初の列でのみ検索します。

次のような大きなファイルがあります。

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

その後、私のスキーマファイルは次のようになります。

denovo1
denovo3
denovo22

私のファイルのパターンと正確に一致する行だけを抽出するために使用しようとしていますfgrep(それで欲しいdenovo1がそうではありませんdenovo11)。正確な一致を試しましたが、空の-xファイルがありました。私は試した:

fgrep -x --file="pattern" bigfile.txt > clusters.blast.uniq

最初の列でのみgrep検索を実行する方法はありますか?

ベストアンサー1

フラグが欲しいかもしれません-wman 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.

つまり

grep -wFf patfile file
denovo1 xxx yyyy oggugu ddddd
denovo22 hhhh yyyy kkkk iiii

最初の列でのみ一致を強制するには、追加するパターンファイルのエントリを変更する必要があります。ラインアンカー\b:コマンドラインスイッチの代わりにアンカーという単語を使用することもできます。-wたとえば、次のようになります。patfile

^denovo1\b
^denovo3\b
^denovo22\b

それから

grep -f patfile file
denovo1 xxx yyyy oggugu ddddd
denovo22 hhhh yyyy kkkk iiii

-Fファイルに単純な固定文字列ではなく正規表現が含まれている場合は、このスイッチを削除する必要があります。

おすすめ記事