[英]execute multiple commands on the same match in sed
I know I can run commands against a match in sed
like so: 我知道我可以针对
sed
中的匹配运行命令,如下所示:
$ cat file
line1
line2
line3
line4
$ sed -e "/^line2/ s/^/# /" file
line1
# line2
line3
line4
Is it possible to run multiple commands, say s/^/#/
and s/$/ # invalid/
on the same match? 是否可以在同一匹配项上运行多个命令,例如
s/^/#/
和s/$/ # invalid/
?
I tried adding them both but I get an error: 我尝试将两者都添加,但出现错误:
$ sed -e "/^line2/ s/^/# / s/$/ # /" file
sed: -e expression #1, char 18: unknown option to `s'
I tried using ;
我尝试使用
;
but that seems to discard the initial match and execute on every line in the input: 但这似乎放弃了初始匹配并在输入的每一行上执行:
$ sed -e "/^line2/ s/^/# / ; s/$/ # /" file
line1 #
# line2 #
line3 #
line4 #
EDIT2: In case you are looking very specifically to execute multiple statements when a condition has met then you could use {..}
block statements as follows.(Just saw @Tiw suggested same thing in comments too.) EDIT2:如果您非常希望在满足条件时执行多条语句,则可以按以下方式使用
{..}
块语句。(刚刚看到@Tiw在注释中也建议了相同的内容。)
sed '/^line2/ {s/^/#/; s/$/ # invalid/}' Input_file
EDIT: Adding 1 more solution with sed
here. 编辑:在此处添加1
sed
更多解决方案。 (I am simply taking line which is starting with line2
in a memory buffer \\1
then simply while putting substituted/new_value adding #
before it and appending # invalid
after it) (我简单地把其起始线
line2
在存储缓冲器\\1
然后简单而把取代的/ NEW_VALUE添加#
之前和附加# invalid
后)
sed '/^line2/s/\(.*\)/# \1 # invalid/' Input_file
Could you please try following. 您可以尝试以下吗?
sed "/^line2/ s/^/# /;/^line2/s/$/ # invalid/" Input_file
What is happening in your attempt, you are simply doing substitution which is happening on each line irrespective of either it starts from line2
or not, so when you give that condition before substitution it should work then. 什么是你尝试发生的事情,只是进行简单地替代其每行发生的事情,不论它要么从开始
line2
或没有,所以当你替换之前,给这个条件应该工作,然后。
您可以尝试使用扩展的正则表达式:
sed -r -e '/^line2/ s/(.*)/# \1 #/' file
您不需要多个命令,只需替换以line2
开头的行:
sed "s/^line2.*/# & #/" file
This might work for you (GNU sed): 这可能对您有用(GNU sed):
sed '/^line2/!b;s/^/.../;s/$/.../' file
Here the regexp is negated by using the !
在这里,正则表达式使用
!
取反!
command and the b
command by itself branches to the end of all sed commands. 命令和
b
命令本身分支到所有sed命令的末尾。 Perhaps as easier example would be: 也许更简单的例子是:
sed ' /^lines/bjumphere;bjumpthere;:jumphere;s/^/.../;s/$/.../;:jumpthere' file
Of course the above can also be written: 当然上面也可以写成:
sed '/^line2/{s/^/.../;s/$/.../}' file
When writing one liners that end in a newline (such as i
, a
, c
, r
, R
, w
and W
commands) and further commands are wanted, the -e
option may be invoked for each part of the sed script, an example: 当编写一个以换行符结尾的衬线(例如
i
, a
, c
, r
, R
, w
和W
命令)并且需要其他命令时,可以为sed脚本的每个部分调用-e
选项,例如:
sed -e '/^line2/{i\a couple of lines\ninserted before "line2"' -e '}' file
Also could be written: 也可以这样写:
sed '/^line2/i\a couple of lines\ninserted before "line2"' file
if there are no subsequent sed commands to be executed, however if line2
needed to be deleted too: 如果有要被执行没有后续sed命令,但是如果
line2
需要被太删除:
sed -e '/^line2/b;!i\a couple of lines\ninserted before "line2 then line2 deleted"' -e 'd' file
However a better way would be to use the c
command. 但是,更好的方法是使用
c
命令。
sed '/^line2/c\a line changed and a line\nappended for "line2"' file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.