mac上执行sed的编辑 -i命令报错sed: 1: "test.txt": undefined label ‘est.txt’或sed: 1: "2a est": extra characters after at the end of a command

问题一
sed编辑命令:【sed -i 's/a/b/g' test.txt】
 
报错:sed: 1: "test.txt": undefined label 'est.txt'
 
解决方案:增加一个备份的追加名【sed -i '.bak' 's/a/b/g' test.txt】
 
原因:mac强制要求备份,否则报错
当然可以不使用其他备份名字,只是用’',就可以只保留一份
sed -i ‘’ ’s/a/b/g’ test.txt
 
问题二
sed追加命令:【sed -i '' "/a/axxx” test.txt】匹配到a字符后追加xxx内容
 
报错:sed: 1: "2a est": extra characters after at the end of a command
 
解决方案:在追加内容前换行,且要用双斜杠\
sed -i '' "/a/a\
xxx" test.txt
 
但是这又有一个新的问题,追加的内容是显示在下一行的前面,没有独立占据一行
使用\n啊 什么的都无效,其实只要在字符串后面直接输入\然后回车换行就有效了,如下图所示。
 
 
备注:在某一行前插入用/i,例如在匹配到a的前面一行加入yyy
sed -i '' "/a/i\
xxx" test.txt
 
 
mac上的sed -n之类的与linux系统上使用方法一样
 
原文地址:https://www.cnblogs.com/meitian/p/5907562.html