【问题标题】:sed command to replace newline character is not working in solaris but working in linux用于替换换行符的 sed 命令在 solaris 中不起作用,但在 linux 中起作用
【发布时间】:2014-04-07 01:00:34
【问题描述】:

我有以下 sed 命令在 linux 机器中工作。但同样不适用于solaris box。请纠正我是什么问题?

a=`sed ':a;N;$!ba;s/\n/ /g' file.txt`

投掷,

-bash-3.00$ a=`sed ':a;N;$!ba;s/\n/ /g' file.txt`
Label too long: :a;N;$!ba;s/\n/ /g

【问题讨论】:

    标签: linux bash unix sed solaris


    【解决方案1】:

    solaris 上使用sed,您必须像这样分解它:

    sed -e ':a' -e 'N;$!ba' -e 's/\n/ /g' file.txt
    

    根据man页面:

    b label  Branch to the : command bearing the label.
                 If label is empty, branch to the end of
                 the script.  Labels are recognized unique
                 up to eight characters.
    

    由于它们最多可识别八个字符,因此如果您的标签比您的标签短,您需要将 sed 拆分为多个表达式。

    【讨论】:

      【解决方案2】:

      在原来的sed 中,我认为标签必须单独一行。从我非常古老的sed & awk Nutshell 书中,它指出:

      标签是最多七个字符的任意序列。标签单独放在一行上,并以冒号开头:

      :label

      这意味着,您需要使用多个-e 参数将其与脚本的其余部分分开,或者查看您的Solaris 机器上是否有nawkgawk。或者,由于您似乎只是想用空格替换所有换行符,因此有更好的工具来完成这项工作,例如 tr,翻译实用程序,它应该至少像 @987654328 一样无处不在@:

      a=`tr '\n' ' ' <file.txt`
      

      【讨论】:

        猜你喜欢
        • 2015-03-24
        • 2014-09-27
        • 1970-01-01
        • 2018-12-27
        • 2019-09-28
        • 1970-01-01
        • 1970-01-01
        • 2012-06-30
        • 2014-03-28
        相关资源
        最近更新 更多