【问题标题】:Using 'sed' find and replace some text and store it in another file使用 'sed' 查找并替换一些文本并将其存储在另一个文件中
【发布时间】:2017-01-24 12:09:48
【问题描述】:

我有一个文件 base_foo.txt 并想替换 base_file 中的一些文本并将其存储在另一个文件中而不更改基本文件。谢谢!

类似:

sed -i -e 'r/foo/bar/' base_foo.txt ???

【问题讨论】:

  • 不要在你的命令中使用-i选项并使用重定向操作符将其发送到其他文件。,

标签: shell unix command-line sed


【解决方案1】:

如果您不想修改原始文件,则必须删除 -i 标志。如果没有-ised 会将其输出发送到stdout,因此您将其重定向到文件。做这样的事情:

sed -e 'r/foo/bar/' base_foo.txt > new_file.txt

【讨论】:

    【解决方案2】:

    -i 在 sed 中就地替换。所以它使用的原始文件将被修改。

    要显示更改,请使用:

    sed -e 's/foo/bar/g' file  #Only to stdout
    

    将更改重定向到新文件:

    sed -e 's/foo/bar/g' file > new.file # changes would be only in new.file
    

    更改原始保存备份

    sed  -i.bak -e 's/foo/bar/g' file  #Changes in file ,no change in file.bak
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-20
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多