我想替换文件的第二行,我知道$ use用于最后一行,但不知道怎么说最后一行.
parallel (
{
ignore(FAILURE) {
build( "Build2Test", BUILDFILE: "", WARFILE: "http://maven.example.com/130602.0.war", STUDY: "UK", BUG: "33323" )
}},
)
Run Code Online (Sandbox Code Playgroud)
我想,以取代}},与}} 总之我想删除,逗号,但这个文件有许多其他的代码,所以我不能使用模式匹配我需要使用二线从文件末尾.
如果你知道,如何改变第N行,只需先反转文件,例如它不像其他sed解决方案那么专业,但是有效... :)
tail -r <file | sed '2s/}},/}}/' | tail -r >newfile
Run Code Online (Sandbox Code Playgroud)
例如,从下一个输入
}},
}},
}},
}},
}},
Run Code Online (Sandbox Code Playgroud)
以上所作
}},
}},
}},
}}
}},
Run Code Online (Sandbox Code Playgroud)
的tail -r是BSD相应的Linux的的tac命令.在Linux上使用tacOS X或Freebsd使用tail -r.Bot做同样的事情:按行的顺序打印文件(最后一行打印为第一行).
反转文件,在第二行上工作,然后重新反转文件:
tac file | sed '2 s/,$//' | tac
Run Code Online (Sandbox Code Playgroud)
要将结果保存回"文件",请将其添加到命令中
> file.new && mv file file.bak && mv file.new file
Run Code Online (Sandbox Code Playgroud)
或者,使用ed脚本
ed file <<END
$-1 s/,$//
w
q
END
Run Code Online (Sandbox Code Playgroud)
以下内容应该有效(请注意,在某些系统上,您可能需要删除所有注释):
sed '1 { # if this is the first line
h # copy to hold space
d # delete pattern space and return to start
}
/^}},$/ { # if this line matches regex /^}},$/
x # exchange pattern and hold space
b # print pattern space and return to start
}
H # append line to hold space
$ { # if this is the last line
x # exchange pattern and hold space
s/^}},/}}/ # replace "}}," at start of pattern space with "}}"
b # print pattern space and return to start
}
d # delete pattern space and return to start'
Run Code Online (Sandbox Code Playgroud)
或者紧凑版:
sed '1{h;d};/^}},$/{x;b};H;${x;s/^}},/}}/;b};d'
Run Code Online (Sandbox Code Playgroud)
例:
$ echo 'parallel (
{
ignore(FAILURE) {
build( "Build2Test", BUILDFILE: "", WARFILE: "http://maven.example.com/130602.0.war", STUDY: "UK", BUG: "33323" )
}},
)' | sed '1{h;d};/^}},$/{x;b};H;${x;s/^}},/}}/;b};d'
parallel (
{
ignore(FAILURE) {
build( "Build2Test", BUILDFILE: "", WARFILE: "http://maven.example.com/130602.0.war", STUDY: "UK", BUG: "33323" )
}}
)
Run Code Online (Sandbox Code Playgroud)