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

时间:2015-10-26 07:32:59

标签: 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 attempt]
a=20
b=20

6 个答案:

答案 0 :(得分:6)

在段落模式下使用awk(记录用空行分隔):

echo form_submit('mysubmit', 'Submit Post!', 'class="btn btn-primary btn-lg btn-block"');

答案 1 :(得分:1)

使用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

答案 2 :(得分:1)

awk -vRS='[' '/first attempt/{printf RT $0}' b

答案 3 :(得分:1)

尝试此sed命令

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

<强>输出:

[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

答案 4 :(得分:1)

这可能适合你(GNU sed):

sed '/^\[second attempt\]/{:a;n;/./ba};d' file

匹配字符串[second attempt]并保持打印直到文件末尾或空行。所有其他行都将被删除。

答案 5 :(得分:0)

sed '/\[second attempt\]/,/^[[:blank:]]*$/ !{d;}' YourFile

仅打印您的部分