【问题标题】:sed -a command not workingsed -a 命令不起作用
【发布时间】:2016-03-18 12:08:40
【问题描述】:

每当我遇到搜索模式时,我都尝试在脚本中执行 sed 命令以在另一行上方插入文本。我尝试了“a”命令来插入文本,但无法正常工作。

sed '/Splitter/a \ "help" '  fileList.out

sed: 1: "/Splitter/a \ "help" ": 在命令末尾的 \ 之后的额外字符

machine-1:Splitter machine-1$ sed '/Splitter  /"help" /a \ '  fileList.out 

sed: 1: "/Splitter /"help" /a \ ": 无效命令代码"

machine-1:Splitter machine-1$ sed '/Splitter  /"help" a \ '  fileList.out 

sed: 1: "/Splitter /"help" a \ ": 无效命令代码"

machine-1:Splitter machine-1$ sed '/Splitter/"help"/a \ '  fileList.out 

sed: 1: "/Splitter/"help"/a \ ": 无效命令代码"

machine-1:Splitter machine-1$ sed '/Splitter/help/a \ '  fileList.out 

sed: 1: "/Splitter/help/a \ ": h 命令末尾的多余字符

machine-1:Splitter machine-1$ sed '/Splitter/a \ '  fileList.out 

//文件列表

./拆分器/视图

./Splitter/应用程序

./Splitter/Util

./Splitter/main

./Splitter/Check/SplitImage

./SplitterTests/SplitterTests

./Splitter/SplitterITests

 machine-1:Splitter machine-1$ sed '/Splitter/a \ '  fileList.out 

【问题讨论】:

    标签: shell sed


    【解决方案1】:

    a 命令需要 \ 后跟 换行符,然后是文本。

    所以命令应该是:

    sed '/Splitter/a\
    "help"'
    

    另请参阅sed man page

    【讨论】:

    • GNU sed 支持单行格式作为扩展。
    • 我需要让这个命令在 shell 脚本中工作。所以不能使用 2 行命令。上面的命令也不会覆盖文件,只是在终端打印输出。
    【解决方案2】:

    要在匹配行上方插入文本,请使用i 命令:

    sed '/Splitter/i \
    "help"
    ' fileList.out
    

    【讨论】:

      猜你喜欢
      • 2014-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      • 2018-11-24
      • 2017-03-05
      • 2020-09-15
      • 1970-01-01
      相关资源
      最近更新 更多