【问题标题】:Replacing lines with carriage return with Sed用 Sed 用回车替换行
【发布时间】:2020-02-05 02:14:01
【问题描述】:

我想修改以下文件:

authorize {
        update request {
                Monthly-Usage = "%{sql:SELECT COALESCE((SUM(`acctoutputoctets`)), 0) FROM radacct WHERE `username`='%{User-Name}' AND Month(acctupdatetime)=(Month(NOW())) AND Year(acctupdatetime)=Year(NOW())}"
        }

    filter_username
    etc


}

这样就变成了这样(更新请求{...}被去掉了)

authorize {

    filter_username
    etc
}

我查阅了一些例子并想出了这个,但它不起作用:

sed -i 'N; s/update request {\nMonthly-Usage = "%{sql:SELECT COALESCE((SUM(`acctoutputoctets`)), 0) FROM radacct WHERE `username`='"'"'%{User-Name}'"'"' AND Month(acctupdatetime)=(Month(NOW())) AND Year(acctupdatetime)=Year(NOW())}"\n}//g' /etc/freeradius/3.0/sites-enabled/default

【问题讨论】:

  • 是authorize 或update request 在文件中的多次出现,您需要保持其他人不变?为什么不像你展示的那样用空白模板重新创建一个新文件?祝你好运。
  • 这行得通:awk '/^}/{f=0} !f; /authorize {/{f=1}' <file>。查看stackoverflow.com/a/58243951/1251660了解更多详情。
  • @shellter 是的,有多个update request,但只有一个authorize {。谢谢
  • @slayedbylucifer 遗憾的是这行不通。 authorize { ... } 中还有其他内容,现在也已删除。

标签: ubuntu sed


【解决方案1】:

这可能对你有用(GNU sed):

sed '/^authorize {$/,/^}$/c\authorize {\n ...\n}' file

使用范围来更改文件。

【讨论】:

  • 抱歉,我没有解释清楚。 ... 仅表示我想保留更多的东西。我只想删除update request { ... }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-09
相关资源
最近更新 更多