MST
星途 面试题库

面试题:Bash中如何替换文件特定字符串

假设有一个文本文件test.txt,内容为'Hello, World! This is a test.',现在需要将其中的'World'替换为'Linux',请写出Bash命令。
27.5万 热度难度
编程语言Bash

知识考点

AI 面试

面试题答案

一键面试
sed 's/World/Linux/' test.txt > new_test.txt

解释:sed 是用于流编辑的工具,s/World/Linux/ 表示将匹配到的 World 替换为 Linuxtest.txt 是源文件,> 用于将结果输出到新文件 new_test.txt 中。如果想直接修改 test.txt 可使用 -i 选项:

sed -i 's/World/Linux/' test.txt