我有这些数据:
One
two
three
Four
five
six
Seven
eight
Run Code Online (Sandbox Code Playgroud)
这个命令:
sed -n '/^Four$/,/^[^[:blank:]]/p'
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
Four
five
six
Seven
Run Code Online (Sandbox Code Playgroud)
如何将此sed表达式更改为与输出的最后一行不匹配?所以理想的输出应该是:
Four
five
six
Run Code Online (Sandbox Code Playgroud)
我尝试了许多涉及感叹号的事情,但还没有设法接近让这个工作.
使用"do..while()"循环:
sed -n '/^Four$/{:a;p;n;/^[[:blank:]]/ba}'
Run Code Online (Sandbox Code Playgroud)
细节:
/^Four$/ {
:a # define the label "a"
p # print the pattern-space
n # load the next line in the pattern space
/^[[:blank:]]/ba # if the pattern succeeds, go to label "a"
}
Run Code Online (Sandbox Code Playgroud)
您可以通过管道连接到另一个sed并跳过最后一行:
sed -n '/^Four$/,/^[^[:blank:]]/p' file | sed '$d'
Run Code Online (Sandbox Code Playgroud)
Four
five
six
Run Code Online (Sandbox Code Playgroud)
或者您可以使用:
sed -n '/^Four$/,/^[^[:blank:]]/{/^Four$/p; /^[^[:blank:]]/!p;}' file
Run Code Online (Sandbox Code Playgroud)