【问题标题】:find and replace each line - bash script查找并替换每一行 - bash 脚本
【发布时间】:2013-11-08 10:06:39
【问题描述】:

我想删除文件中以“#”开头的每一行。我跑了(我正在使用 osx)

 sed -i '' -e 's/#.*/d' file

但我收到此错误消息:

sed: 1: "s/#.*/d
": unescaped newline inside substitute pattern

【问题讨论】:

  • 你需要sed -i '/^#/d' file
  • @gniourf_gniourf: sed -i '/^#/d' filesed: 1: "file": invalid command code f
  • @GarethRees 你可能没有处理 -i 选项的 GNU sed...
  • @GarethRees 有趣的是,我的 GNU sed 对我给出的命令很满意......你确定你没有打错吗?此外,该命令正是您在答案:D 中给出的命令。
  • OP 在 OS X 上,sed 是 BSD-ish,这意味着 -i 选项需要一个参数。

标签: macos bash replace


【解决方案1】:

sed 中的 s 命令表示“替代”,它需要 两个 参数:

s/pattern/replacement/

你要做的只是匹配#开头的行并删除它们,所以你需要sed程序:

/^#/d

请注意,模式需要以^ (meaning "start of line") 开头,否则它将匹配行中任意位置的#

【讨论】:

    【解决方案2】:

    正如上面 Gareth Rees 所说,正确的命令是:

    sed '/^#/ d' file
    

    这个很好的sed 教程以您的问题为例:

    http://www.grymoire.com/Unix/Sed.html#toc-uh-30

    【讨论】:

      猜你喜欢
      • 2018-09-07
      • 2015-02-01
      • 1970-01-01
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-17
      相关资源
      最近更新 更多