0

我有一个大的 txt 文件,我正在寻找以物种名称“ABS”开头的 seq id。当我执行 grep "ABS" 时,我只得到 ABS 列表,而不是 seq id 后跟该词。例如列出我正在寻找的内容是这样的:

ABS|contig05671,
ABS|contig04453,
ABS|CL5170Contig1,
ABS|contig02526,

但是,当我这样做时,grep "ABS" filename.txt,我得到如下结果:

ABS,
ABS,
ABS,
ABS,

任何帮助是极大的赞赏。提前致谢。

4

1 回答 1

1

来自man grep

Context Line Control
   -A NUM, --after-context=NUM
          Print NUM  lines  of  trailing  context  after  matching  lines.
          Places   a  line  containing  a  group  separator  (--)  between
          contiguous groups of matches.  With the  -o  or  --only-matching
          option, this has no effect and a warning is given.

   -B NUM, --before-context=NUM
          Print  NUM  lines  of  leading  context  before  matching lines.
          Places  a  line  containing  a  group  separator  (--)   between
          contiguous  groups  of  matches.  With the -o or --only-matching
          option, this has no effect and a warning is given.

   -C NUM, -NUM, --context=NUM
          Print NUM lines of output context.  Places a line  containing  a
          group separator (--) between contiguous groups of matches.  With
          the -o or --only-matching option,  this  has  no  effect  and  a
          warning is given.

因此,如果您需要匹配行和下一行,则需要grep -A1 ABS file.txt,并且对于上一行,同样需要-B1.

但是,如果您想以另一种方式格式化结果(例如,将两行放在一个并用竖线字符分隔),您需要一个不同于grep. grep进行搜索,而您还需要编辑。

于 2013-09-16T19:15:16.013 回答