「chr1」で始まるエントリを削除し、「chr11」または「chr19」で始まるエントリを保持するにはどうすればgrepしますか?

「chr1」で始まるエントリを削除し、「chr11」または「chr19」で始まるエントリを保持するにはどうすればgrepしますか?

次の項目を含むファイルがあります。

chr1    740678  740720
chr1    2917480 2917507

orで始まる項目を削除し、orなどで始まる項目は維持したいとchr1思います。それを使用すると、chr11またはchr19で始まる他のすべての項目が削除されます。使用できる他の正規表現はありますか?chr11chr19grep -v "chr1"

ベストアンサー1

まず、最初の文字列を含むが最初の文字列ではない行を見つける^chr1ことを避けるために、行の先頭()にのみ一致するように正規表現を固定する必要があります(たとえば、コメント付きのVCFファイルで簡単に発生する可能性があります)。次に、(GNU)オプションをchr1使用できます。-wgrep

   -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.   This  option
          has no effect if -x is also specified.

これをサポートしていない場合は、grep以下を使用してください。

grep -v '^chr1\s' file

スペース(タブとスペースを含む)と一致するため、スペース\s文字で始まり、chr1その後にスペース文字が続くすべての行は除外されます。

おすすめ記事