我意识到这!在命令行历史上下文中对命令行具有特殊意义,但除此之外,在运行脚本中,感叹号有时会导致解析错误。
我认为它与 an 有关event,但我不知道事件是什么或它做什么。即便如此,相同的命令在不同情况下的行为也会有所不同。
下面的最后一个示例导致错误;但是为什么,当相同的代码在命令替换之外工作时呢?.. 使用 GNU bash 4.1.5
# This works, with or without a space between ! and p
{ echo -e "foo\nbar" | sed -nre '/foo/! p'
echo -e "foo\nbar" | sed -nre '/foo/!p'; }
# bar
# bar
# This works, works when there is a space between ! and p
var="$(echo -e "foo\nbar" | sed -nre '/foo/! p')"; echo "$var"
# bar
# This causes an ERROR, with NO …Run Code Online (Sandbox Code Playgroud) 每次找到匹配项时,我都想打印上一行。我知道grep -A和-B选项。但是我的 Solaris 5.10 机器不支持这些选项。
我想要只使用sed.
Foo.txt:
Name is : sara
age is : 10
Name is : john
age is : 20
Name is : Ron
age is : 10
Name is : peggy
age is : 30
Run Code Online (Sandbox Code Playgroud)
Out.txt:
Name is : sara
Name is : Ron
Run Code Online (Sandbox Code Playgroud)
我试图匹配的模式是age is : 10.
我的环境,Solaris 5.10。