加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – sed替换文件的第二行

发布时间:2020-12-14 06:06:50 所属栏目:百科 来源:网络整理
导读:我想替换文件的第二行,我知道$use用于最后一行,但不知道怎么说最后一行. parallel ({ignore(FAILURE) {build( "Build2Test",BUILDFILE: "",WARFILE: "http://maven.example.com/130602.0.war",STUDY: "UK",BUG: "33323" )}},) 我想替换}},用}}简而言之我想删
我想替换文件的第二行,我知道$use用于最后一行,但不知道怎么说最后一行.

parallel (
{
ignore(FAILURE) {
build( "Build2Test",BUILDFILE: "",WARFILE: "http://maven.example.com/130602.0.war",STUDY: "UK",BUG: "33323" )
}},)

我想替换}},用}}简而言之我想删除,逗号,但这个文件有很多其他代码所以我不能使用模式匹配我需要使用文件末尾的第二行.

解决方法

以下内容应该有效(请注意,在某些系统上,您可能需要删除所有注释):

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'

或者紧凑版:

sed '1{h;d};/^}},$/{x;b};H;${x;s/^}},/}}/;b};d'

例:

$echo 'parallel (
{
ignore(FAILURE) {
build( "Build2Test",)' | sed '1{h;d};/^}},/}}/;b};d'
parallel (
{
ignore(FAILURE) {
build( "Build2Test",BUG: "33323" )
}}
)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读