我有一个文件,里面有这样的条目:
chr1 740678 740720
chr1 2917480 2917507我想删除以chr1开头的条目,但保留以chr11或chr19开头的其他条目,等等。当我使用grep -v "chr1"时,它会删除以chr11或chr19开头的其他部分。我可以使用另一个正则表达式吗?
发布于 2020-08-05 10:53:56
首先,您应该锚定正则表达式,使其仅在行(^chr1)的开头匹配,以避免查找包含chr1的行,但这不是第一个字符串(例如,对于带注释的VCF文件,这很容易发生)。接下来,您可以将-w选项用于(GNU) 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. This option
has no effect if -x is also specified.如果您的grep不支持这一点,那么请使用以下命令:
grep -v '^chr1\s' file\s匹配空白(包括制表符和空格),因此将排除以chr1开头的任何行,然后排除任何类型的空格字符。
发布于 2020-08-05 10:45:15
看起来在chr1之后有一些空格或制表符。所以您可以搜索chr1,后面跟着一些空格字符。试试这个:
grep -v "chr1\s\+"https://unix.stackexchange.com/questions/602953
复制相似问题