首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何删除以"chr1“开头但保留"chr11”或"chr19“的条目?

如何删除以"chr1“开头但保留"chr11”或"chr19“的条目?
EN

Unix & Linux用户
提问于 2020-08-05 10:38:45
回答 2查看 645关注 0票数 2

我有一个文件,里面有这样的条目:

代码语言:javascript
复制
chr1    740678  740720
chr1    2917480 2917507

我想删除以chr1开头的条目,但保留以chr11chr19开头的其他条目,等等。当我使用grep -v "chr1"时,它会删除以chr11或chr19开头的其他部分。我可以使用另一个正则表达式吗?

EN

回答 2

Unix & Linux用户

回答已采纳

发布于 2020-08-05 10:53:56

首先,您应该锚定正则表达式,使其仅在行(^chr1)的开头匹配,以避免查找包含chr1的行,但这不是第一个字符串(例如,对于带注释的VCF文件,这很容易发生)。接下来,您可以将-w选项用于(GNU) grep

代码语言:javascript
复制
   -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不支持这一点,那么请使用以下命令:

代码语言:javascript
复制
grep -v '^chr1\s' file

\s匹配空白(包括制表符和空格),因此将排除以chr1开头的任何行,然后排除任何类型的空格字符。

票数 6
EN

Unix & Linux用户

发布于 2020-08-05 10:45:15

看起来在chr1之后有一些空格或制表符。所以您可以搜索chr1,后面跟着一些空格字符。试试这个:

代码语言:javascript
复制
grep -v "chr1\s\+"
票数 4
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/602953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档