[英]Bash, Using grep, sed or awk to extract section of text and then match
I have a text file and want to extract all interfaces matching "blue" 我有一个文本文件,想提取所有匹配“蓝色”的接口
random text random text random text
random text random text
int 1
random text
blue
random text
random text
int 2
random text
random text
red
random text
int 3
random text
random text
random text
blue
random text
random text
int 4
blue
random text
int n
random text
value
random text
random text random text random text
random text random text
Wanted output: 想要的输出:
int 1
blue
int 3
blue
int 4
blue
int n
blue
(notice int 2 is "red" and therefore not displayed) (注意,int 2为“红色”,因此不显示)
I've tried: grep "int " -A n file.txt | 我试过了:grep“ int” -A n file.txt | grep "blue" but that only display lines matching "blue".
grep“ blue”,但只显示与“ blue”匹配的行。 I want to also show the lines matching "int ".
我还要显示匹配“ int”的行。 Also the section length can vary so using -A n hasn't been useful.
另外,段的长度可以变化,因此使用-A n并没有用。
An awk solution could be the following: 一个awk解决方案可能是以下几种:
awk '/^int/{interface = $0} /blue/{print interface; print $0}' input.txt
It always saves the latest discovered interface. 它总是保存最新发现的接口。 If
blue
is found, it prints the stored interface and the line containing blue
. 如果找到
blue
,它将打印存储的接口和包含blue
的行。
Another sed solution 另一个sed解决方案
Will work for multiple blues 适用于多个蓝调
sed -n '/^int/{x;/blue/{p;d}};/blue/H' file
random text random text random text
random text random text
int 1
random text
blue
blue
random text
random text
int 2
random text
random text
red
random text
int 3
random text
random text
random text
blue
random text
random text
int 4
blue
blue
blue
blue
blue
random text
int n
random text
value
random text
random text random text random text
random text random text
int 1
blue
blue
int 3
blue
int 4
blue
blue
blue
blue
blue
one possible GNU sed solution 一种可能的GNU sed解决方案
sed -n '/^int\|blue/p' file | sed -r ':a; N; $! ba; s/int \w*\n(int)/\1/g; s/int \w*$//'
output 输出
int 1
blue
int 3
blue
int 4
blue
sed '/^int/ h
/^[[:space:]]*blue/ {x;G;p;}
d
' YourFile
added (post) constraint 添加(发布)约束
Explication: 解释:
{x;G;p;}
(other action give the same depending of any other interest like H;x;p
or H;g;p
, in this case this is header destructive but it could be conservative using a s///
) {x;G;p;}
(其他操作会根据H;x;p
或H;g;p
不同给出相同的结果H;g;p
,在这种情况下,这是标头破坏性的,但使用s///
可以是保守的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.