在 OSX 中使用 grep 和 sed 查找并替换

chr*_*gos 3 macos terminal sed

#archive我正在尝试将同一目录中的一堆文件中的字符串替换为~archive. 我的文件名中有空格并且相当长。我查了一下,有人说我必须用 -Z 调用 grep ,用 -0 调用 xargs 。我也这么做了。

我当前的终端命令是这样的:

grep -lZ '#archive' ~/dropbox/documents/notes/Archive/* | xargs -0 sed -i "" 's|#archive|~archive|g'
Run Code Online (Sandbox Code Playgroud)

我得到的输出是这样的:

sed: /Users/bobthebuilder/dropbox/documents/notes/Archive/20130824_1553_I_Kino Interesting Books.txt
/Users/bobthebuilder/dropbox/documents/notes/Archive/20131003_1011_R_ExpensesOct13.txt
/Users/bobthebuilder/dropbox/documents/notes/Archive/20131111_1205_RI_SocialConnect.txt
/Users/bobthebuilder/dropbox/documents/notes/Archive/20131111_2233_I_GymNov13Dec13.txt
/Users/bobthebuilder/dropbox/documents/notes/Archive/20131112_1121_I_LifeChallenge.txt
/Users/bobthebuilder/dropbox/documents/notes/Archive/20131112_1242__PaperlessLiving.txt
/Users/bobthebuilder/dropbox/documents/notes/Archive/20131112_1243__Polymath.txt
/Users/bobthebuilder/dropbox/documents/notes/Archive/20131112_1327__ORDPlanning.txt
/Users/bobthebuilder/dropbox/documents/notes/Archive/20131124_1126__YunheProjectExcel.txt
/Users/bobthebuilder/dropbox/documents/notes/Archive/Contact Information.txt
/Users/bobthebuilder/dropbox/documents/notes/Archive/Excruciatingly Useful Shortcuts.txt
/Users/bobthebuilder/dropbox/documents/notes/Archive/How does this thing work?.txt
/Users/bobthebuilder/dropbox/documents/notes/Archive/Mail Migration.txt
/Users/bobthebuilder/dropbox/documents/notes/Archive/Markdownify.txt
/Users/bobthebuilder/dropbox/documents/notes/Archive/This is the title of a note.txt
: File name too long
Run Code Online (Sandbox Code Playgroud)

并且没有任何东西被替换。

我也尝试过:

| => grep -l --null '#archive' ~/dropbox/documents/notes/Archive/* | xargs -0 sed -i "" 's|#archive|~archive|g'
sed: RE error: illegal byte sequence
Run Code Online (Sandbox Code Playgroud)

帮助将不胜感激!

Ner*_*elu 5

如果文件位于同一文件夹中

sed -i "" 's/#archive/~archive/' ~/dropbox/documents/notes/Archive/*
Run Code Online (Sandbox Code Playgroud)