【问题标题】:How to output the contents of a variable to a newline using sed如何使用 sed 将变量的内容输出到换行符
【发布时间】:2014-02-24 21:23:30
【问题描述】:

PING=$(ping -c 2 google.com)

sed -i.bak "s/test:/&\n$PING/g" test.txt

我试图在 test.txt 文件中测试后在换行符上输出变量 PING。 但是我一直收到这个错误。

sed: -e expression #1, char 64: unterminated `s' command

我不知道我哪里出错了,非常感谢任何帮助。

【问题讨论】:

    标签: sed


    【解决方案1】:

    您可以像编写 bash 脚本一样编写 sed 脚本,而换行符是命令分隔符。你需要一个续行。

    未经测试:

    sed "s/test/&\
    $PING/g" file
    

    风格建议:改掉使用大写变量名的习惯。有一天你会使用 PATH 并破坏你的脚本。

    【讨论】:

      猜你喜欢
      • 2018-08-29
      • 1970-01-01
      • 2012-02-15
      • 2011-04-30
      • 2011-01-16
      • 1970-01-01
      • 2011-10-08
      • 2020-07-29
      • 1970-01-01
      相关资源
      最近更新 更多