問題一
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/a\xxx” test.txt】匹配到a字符后追加xxx內容
報錯:sed: 1: "2a\test\": extra characters after \ at the end of a command
解決方案:在追加內容前換行,且要用雙斜杠\\
sed -i '' "/a/a\\
xxx" test.txt
但是這又有一個新的問題,追加的內容是顯示在下一行的前面,沒有獨立占據一行

使用\\n啊\n什么的都無效,其實只要在字符串后面直接輸入\\然后回車換行就有效了,如下圖所示。

備注:在某一行前插入用/i,例如在匹配到a的前面一行加入yyy
sed -i '' "/a/i\\
xxx" test.txt
mac上的sed -n之類的與linux系統上使用方法一樣