【问题标题】:unable to insert text at a particular line of a file using linux sed无法使用 linux sed 在文件的特定行插入文本
【发布时间】:2013-01-16 15:05:14
【问题描述】:

我想使用以下命令使用 linux sed 替换文件的第二行。

sed -i"" '2d' /usr/local/services/status.sh
sed -i '2i\testing' /usr/local/services/status.sh

我的命令有错误吗?我查不出来。 还有其他方法吗?

感谢任何形式的帮助。

【问题讨论】:

    标签: linux sed


    【解决方案1】:

    这可能对你有用:

    sed -i '2c\testing' file
    

    【讨论】:

      【解决方案2】:

      替换第二行:

      $ cat file
      AIX
      Unix
      Linux
      $ sed -i '2s/.*/testing/' file
      $ cat file
      AIX
      testing
      Linux
      

      在第二行之前插入:

      $ sed -i '2i testing' file
      

      使用变量作为替代的一部分:

      $ PID=2456
      $ sed -i "2s/.*/testing $PID/" file
      

      【讨论】:

      • 非常感谢。你能告诉我如何在 /.*/____/ 之间连接两个字符串吗?例如:我需要将 PID 与文本一起保存。
      • 例如:我需要将 PID 与文本一起保存。类似这样的东西-sed -i '2s/.*/ps -p $! > /dev/null 2>&1 && echo 1 正在运行 || echo 1 not running' 请纠正我。
      • 查看更新..首先将 PID 存储在单独的变量中,然后在 sed 命令中使用该变量。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      • 2017-12-07
      • 2011-05-04
      • 1970-01-01
      • 2021-06-17
      • 1970-01-01
      相关资源
      最近更新 更多