I have a config file like this:
我有这样的配置文件:
[whatever]
Do I need this? no!
[directive]
This lines I want
Very much text here
So interesting
[otherdirective]
I dont care about this one anymore
Now I want to match the lines in between [directive]
and [otherdirective]
without matching [directive]
or [otherdirective]
.
现在我想匹配[directive]和[otherdirective]之间的行,而不匹配[directive]或[otherdirective]。
Also if [otherdirective]
is not found all lines till the end of file should be returned. The [...]
might contain any number or letter.
如果没有找到[otherdirective],那么在文件结束前所有的行都应该被返回。(…可能包含任何数字或字母。
Attempt
I tried this using sed
like this:
我使用sed进行了如下尝试:
sed -r '/\[directive\]/,/\[[[:alnum:]+\]/!d
The only problem with this attempt is that the first line is [directive]
and the last line is [otherdirective]
.
这个尝试的唯一问题是第一行是[指令],最后一行是[其他指令]。
I know how to pipe this again to truncate the first and last line but is there a sed
solution to this?
我知道如何再次使用管道来截断第一行和最后一行,但是有一个sed解决方案吗?
4 个解决方案
#1
2
You can use the range, as you were trying, and inside it use //
negated. When it's empty it reuses last regular expression matched, so it will skip both edge lines:
您可以使用范围,如您所尝试的,并且在其内部使用//否定。当它为空时,它重复使用最后匹配的正则表达式,因此它将跳过两条边:
sed -n '/\[directive\]/,/\[otherdirective\]/ { //! p }' infile
It yields:
它的收益率:
This lines I want
Very much text here
So interesting
#2
1
Here is a nice way with awk
to get section of data.
这是awk获取数据的好方法。
awk -v RS= '/\[directive\]/' file
[directive]
This lines I want
Very much text here
So interesting
When setting RS to nothing RS=
it divides the file up in records based on blank line.
So when searching for [directive]
it will print that record.
Normally a record is one line, but due to the RS (record selector) is change, it gives the block.
当将RS设置为空时,RS=将文件按空行划分为记录。因此,当搜索[指令]时,它将打印该记录。通常一个记录是一行,但是由于RS(记录选择器)是更改的,所以它给出了块。
#3
0
Okay damn after more tries I found the solution or merely one solution:
好吧,经过更多的尝试,我找到了解决方案或者仅仅是一个解决方案:
sed -rn '/\[buildout\]/,/\[[[:alnum:]]+\]/{
/\[[[:alnum:]]+\]/d
p }'
#1
2
You can use the range, as you were trying, and inside it use //
negated. When it's empty it reuses last regular expression matched, so it will skip both edge lines:
您可以使用范围,如您所尝试的,并且在其内部使用//否定。当它为空时,它重复使用最后匹配的正则表达式,因此它将跳过两条边:
sed -n '/\[directive\]/,/\[otherdirective\]/ { //! p }' infile
It yields:
它的收益率:
This lines I want
Very much text here
So interesting
#2
1
Here is a nice way with awk
to get section of data.
这是awk获取数据的好方法。
awk -v RS= '/\[directive\]/' file
[directive]
This lines I want
Very much text here
So interesting
When setting RS to nothing RS=
it divides the file up in records based on blank line.
So when searching for [directive]
it will print that record.
Normally a record is one line, but due to the RS (record selector) is change, it gives the block.
当将RS设置为空时,RS=将文件按空行划分为记录。因此,当搜索[指令]时,它将打印该记录。通常一个记录是一行,但是由于RS(记录选择器)是更改的,所以它给出了块。
#3
0
Okay damn after more tries I found the solution or merely one solution:
好吧,经过更多的尝试,我找到了解决方案或者仅仅是一个解决方案:
sed -rn '/\[buildout\]/,/\[[[:alnum:]]+\]/{
/\[[[:alnum:]]+\]/d
p }'