【问题标题】:Remove part of url and keep the rest using sed删除部分 url 并使用 sed 保留其余部分
【发布时间】:2020-04-06 18:00:24
【问题描述】:

需要删除部分 url 并保持其余部分不变。 以下是我的文本文件的一部分,其中包含这样的内容

{
  host = "http://nxt-newepisode.xcfm.crata.dive.com/err1.2.2/table/kenny.xml.gz"

}

{
  host = "http://nxt-secondepisode.xcfm.crata.dive.com/err1.2.2/table/kenny.xml.gz"
}

我想从上面的两个网址中摆脱 nxt- 并保留其余的。我知道使用

sed -i 's/nxt-//g' FILE

可以解决问题,但我想具体一点,只删除第一个 url 的 nxt- 和第二个 url 的 nxt- 而不更改文件的其余部分。

我正在尝试这个

sed -i '/host/s#"http://nxt-newepisode.*"#" "#' FILE

【问题讨论】:

  • 这里有人给出了这个答案 sed -i -E 's~^(\s*host\s+=\s+"http://)nxt-((new|second)episode)~ \1\2~' 文件。这行得通。需要解释一下

标签: regex sed


【解决方案1】:

请您尝试关注一下。

awk '/host/ && ++count==1{sub(/nxt-/,"")} 1' Input_file

一旦您对结果感到满意,然后将输出保存到 Input_file 中,请使用以下命令:

awk '/host/ && ++count==1{sub(/nxt-/,"")} 1' Input_file > temp && mv temp Input_file

【讨论】:

  • 寻找 sed 解决方案。 sed 可以吗?
猜你喜欢
  • 1970-01-01
  • 2015-06-13
  • 1970-01-01
  • 1970-01-01
  • 2022-12-10
  • 2012-07-17
  • 2016-12-11
  • 2023-03-10
  • 1970-01-01
相关资源
最近更新 更多