grep查看匹配行的上下行

grep是一个在文件中查找匹配字符串很有帮助的命令。指到现在,我才知道如何使用grep查看匹配字串的上下行内容。

让我们看一个例子

ALICE was beginning to get
very tired of sitting by
her sister on the bank
and of having nothing to do:
once or twice she had peeped
into the book her sister was reading,
but it had no pictures or conversations in it,
"and what is the use of a book," thought Alice,
"without pictures or conversations?'

如果我们现在要搜索带"bank”的行,我们应该这样写

# grep bank test.txt

然后我们会得到

her sister on the bank

如果我们现在想要得到这行,和这行前面的一行,我们应该这样写

# grep -B1 bank test.txt

得到的结果是这样的

very tired of sitting by
her sister on the bank

如果我们要得到包含"bank”的行和其后面的两行,我们应该这样写

# grep -A2 bank test.txt

这样得到的结果是这样的

her sister on the bank
and of having nothing to do:
once or twice she had peeped

是不是很方便,当然这两个参数还可以同时使用。至于例子,这里就不写了,留着读者自己去探索吧。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值