如何使用linux命令单独显示子标题下的行,而忽略文件中的其他行

如何使用linux命令单独显示子标题下的行,而忽略文件中的其他行,linux,awk,sed,Linux,Awk,Sed,我有一个包含以下内容的文件。在其中,我想单独显示特定子标题下的行。我试着使用一些sed和awk命令,但我没有得到它。任何人都可以用一些linux命令来破解这个问题 [first attempt] a=10 b=20 [second attempt] a=20 b=20 [third attempt ] a=30 b=50 通过使用awk或sed或cut命令指定侧标题,是否可以单独显示“[第二次尝试]”侧标题下的内容或单独显示任何其他侧标题下的内容。输出应如下所示。提前谢谢 [second

我有一个包含以下内容的文件。在其中,我想单独显示特定子标题下的行。我试着使用一些sed和awk命令,但我没有得到它。任何人都可以用一些linux命令来破解这个问题

[first attempt]
a=10
b=20

[second attempt]
a=20
b=20

[third attempt ] 
a=30
b=50
通过使用awk或sed或cut命令指定侧标题,是否可以单独显示“[第二次尝试]”侧标题下的内容或单独显示任何其他侧标题下的内容。输出应如下所示。提前谢谢

[second attempt]
a=20
b=20
使用
awk

awk '/\[first attempt\]/{p=1} !NF{p=0}p' file
测试:

$ awk '/\[second attempt\]/{p=1} !NF{p=0}p' file
[second attempt]
a=20
b=20
[second attempt]
a=20
b=20
sed   '/\[second attempt\]/{:a ; N ;s/.*\n$//;   b a }'
[first attempt]
a=10
b=20


[third attempt ] 
a=30
b=50
在段落模式下使用awk(记录用空行分隔):


也可以尝试此
sed
命令

sed -n  '/\[second attempt\]/{:loop ; N ; /\n$/p ; b loop }'  FileName
输出:

$ awk '/\[second attempt\]/{p=1} !NF{p=0}p' file
[second attempt]
a=20
b=20
[second attempt]
a=20
b=20
sed   '/\[second attempt\]/{:a ; N ;s/.*\n$//;   b a }'
[first attempt]
a=10
b=20


[third attempt ] 
a=30
b=50
忽略[第二次尝试]下的内容:

$ awk '/\[second attempt\]/{p=1} !NF{p=0}p' file
[second attempt]
a=20
b=20
[second attempt]
a=20
b=20
sed   '/\[second attempt\]/{:a ; N ;s/.*\n$//;   b a }'
[first attempt]
a=10
b=20


[third attempt ] 
a=30
b=50
输出:

$ awk '/\[second attempt\]/{p=1} !NF{p=0}p' file
[second attempt]
a=20
b=20
[second attempt]
a=20
b=20
sed   '/\[second attempt\]/{:a ; N ;s/.*\n$//;   b a }'
[first attempt]
a=10
b=20


[third attempt ] 
a=30
b=50
仅打印您的部分

这可能适合您(GNU-sed):


匹配字符串
[第二次尝试]
,并一直打印到文件结尾或空行。所有其他行都将被删除。

您应该说明由于
RT
的原因,该行是特定于gawk的。永远不要打印$0,但要打印“%s”,而不要打印$0。如果代码< 0美元<代码>包含<代码> %s>代码>或其他一些Prtf格式字符,请考虑不同之处。非常感谢,这对我很有帮助。另外,我还有一个要求,你能帮我忽略[首次尝试]子标题下的内容并显示该标题中的所有其他内容吗file@VIJU使用Ed Morton方法。awk-v RS='/第一次/'filenameawk-vRS='[''./第二次尝试/{printf$0 RT}'filename您可以使用
p
而不是
p{print}
,因为
{print}
是默认操作。非常感谢,这对我很有帮助。另外我还有一个要求,你能帮我忽略[second trument]下的内容吗子标题,并使用sed显示文件中的所有其他内容