【问题标题】:sed changes are lost (while running cat command on txt file) [duplicate]sed 更改丢失(在 txt 文件上运行 cat 命令时)[重复]
【发布时间】:2016-12-06 12:55:41
【问题描述】:

我需要在 test.txt 文件的第 4 行插入一个命令“新文件”。

试过sed;我可以看到更改后的文件输出,但是当我再次执行cat test.txt 时,更改就消失了。

sed "4i new file" /test.txt

如何保存更改?

【问题讨论】:

    标签: linux sed


    【解决方案1】:

    使用就地编辑选项sed -i "4i new file" test.txt

    没有-i 选项sed 不会对文件进行任何更改。它只会打印结果。

    -i[SUFFIX], --in-place[=SUFFIX]
        edit files in place (makes backup if SUFFIX supplied)
    

    【讨论】:

      【解决方案2】:

      sed '4i 新文件' test.txt > tmp && mv tmp test.txt

      【讨论】:

        猜你喜欢
        • 2021-04-28
        • 2013-06-05
        • 2015-04-16
        • 1970-01-01
        • 2023-03-30
        • 2016-08-08
        • 2022-01-25
        • 2012-11-14
        • 2011-12-07
        相关资源
        最近更新 更多