linux - 查找字符串并将替换的字符串存储在其他文件 shell 脚本中

标签 linux bash shell unix

我想使用 shell 脚本在文件 sat '1.txt' 中找到一个字符串 say 'foo' 并将 'foo' 替换为 'bar' 并将输出存储到另一个文件 say '2.txt' 而不做任何修改在 1.txt 中。

所以 '1.txt' 将包含 'foo' 本身,但 '2.txt' 现在将包含 '1.txt' 的所有内容,'foo' 替换为 'bar'

我在 bash 中使用这个命令

sed -i "s/foo/bar/g" "1.txt" > 2.txt

但它不起作用。

最佳答案

删除 -i 选项,因为它代表就地操作。

-i[SUFFIX], --in-place[=SUFFIX]

      edit files in place (makes backup if extension supplied)

(sed(1) 手册页)

关于linux - 查找字符串并将替换的字符串存储在其他文件 shell 脚本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37176261/

相关文章:

linux - 如何在启动过程后切换 linux 内核控制台?

bash - 查找与给定模式匹配并排除给定模式的所有文件?

bash - 视频转换后如何命名输出文件与输入文件相同但扩展名不同?

linux - 将csv文件转换为mysql插入语句

android - 如何在没有root权限的情况下运行Android系统应用程序?

linux - 使用 sftp 在本地计算机 (Linux) 中创建目录

linux - Cron 作业未运行

linux - 如何在 C-shell 别名中使用 awk?

linux - shell脚本中的三重嵌套引号

python - Windows subprocess.Popen 没有 shell=True 的批处理文件