Bash,在指定字符串的两行之间使用 grep [英] Bash, grep between two lines with specified string

查看:25
本文介绍了Bash,在指定字符串的两行之间使用 grep的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

a43测试1美国广播公司简历纳米测试2kfo

我需要 test1 和 test2 之间的所有行.在这种情况下,普通的 grep 不起作用.你有什么建议吗?

解决方案

test1打印到test2(包括触发线)

awk '/test1/{f=1}/test2/{f=0;print} f'awk '/test1/{f=1} f;/test2/{f=0}'awk '/test1/,/test2/'

test1美国广播公司简历纳米测试2

<小时><块引用>

打印 test1test2 之间的数据(不包括触发线)

awk '/test1/{f=1;next}/test2/{f=0} f'awk '/test2/{f=0} f;/test1/{f=1}'

abc简历纳米

Example:

a43
test1
abc
cvb
bnm
test2
kfo

I need all lines between test1 and test2. Normal grep does not work in this case. Do you have any propositions?

解决方案

Print from test1 to test2 (Trigger lines included)

awk '/test1/{f=1} /test2/{f=0;print} f'
awk '/test1/{f=1} f; /test2/{f=0}' 
awk '/test1/,/test2/'

test1
abc
cvb
bnm
test2


Prints data between test1 to test2 (Trigger lines excluded)

awk '/test1/{f=1;next} /test2/{f=0} f' 
awk '/test2/{f=0} f; /test1/{f=1}' 

abc
cvb
bnm

这篇关于Bash,在指定字符串的两行之间使用 grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆