在MacOS中的Shell脚本执行sed -i命令报错

1,193 阅读1分钟

MacOS sed -i命令报错

需要删除test.txt中的所有“hello world”字符串,命令如下,

sed -i '/hello world/d' test.txt

但执行时会报错,报错信息如下:

sed: 1: "test.txt": undefined label 'est.txt'

原因: Mac中用-i命令进行替换时,强制要求备份,-i要加一个备份文件路径的参数

sed -i 'copypath' '/hello world/d' test.txt

如果不想备份,路径填空就行

sed -i '' '/hello world/d' test.txt