【问题标题】:Whats the difference between sed -E and sed -esed -E 和 sed -e 有什么区别
【发布时间】:2010-06-29 08:47:35
【问题描述】:

我正在处理一些旧代码,我发现我曾经使用过

sed -E 's/findText/replaceWith/g' #findText would contain a regex

但我现在试试

sed -e 's/findText/replaceWith/g'

它似乎做同样的事情,或者是吗? 我有点记得我这样做是有原因的,但我不记得了,做“man sed”并没有帮助,因为他们没有关于 -E only -e 的任何东西,这对以太没有多大意义。

-e, --expression=script
  Append the editing commands in script to the end of 
  the editing command script. script may contain more 
  than one newline separated command.

我认为 -e 意味着它会与正则表达式匹配...

GNU sed version 4.2.1

【问题讨论】:

    标签: regex linux sed debian


    【解决方案1】:

    从源代码来看,-E 是与 BSD sed 兼容的未记录选项。

    /* Undocumented, for compatibility with BSD sed.  */
    case 'E':
    case 'r':
      if (extended_regexp_flags)
        usage(4);
      extended_regexp_flags = REG_EXTENDED;
      break;
    

    manual开始,BSD中的-E sed用于支持扩展正则表达式。

    【讨论】:

      【解决方案2】:

      来自sed's documentation

      -E

      -r

      --正则表达式扩展

      使用扩展的正则表达式而不是基本的正则表达式。扩展的正则表达式是 egrep 接受的;它们可以更清晰,因为它们通常具有较少的反斜杠。从历史上看,这是一个 GNU 扩展,但 -E 扩展已被添加到 POSIX 标准 (http://austingroupbugs.net/view.php?id=528) 中,因此使用 -E 以获得可移植性。 GNU sed 多年来一直接受 -E 作为未记录的选项,*BSD seds 也接受 -E 多年,但使用 -E 的脚本可能无法移植到其他旧系统。请参阅扩展正则表达式。

      因此,-E 似乎应该是声明您将使用 (E) 扩展正则表达式的首选方式,而不是 -r

      相反,-e 只是指定以下是您要使用sed 执行的脚本(类似于's/bla/abl/g')。

      始终来自文档:

      如果没有 -e 或 -f 选项,sed 使用第一个非选项参数作为脚本,并使用以下非选项参数作为输入文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-04
        • 2013-06-12
        • 1970-01-01
        • 2010-11-24
        • 2015-05-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多