来自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
进行搜索,而您还需要编辑。