我正在尝试查找一个文件,然后编辑这些文件中的文本。这是我到目前为止所拥有的,但遗憾的是我不太熟悉。请帮助?
grep -rl "wrongtext" ~/Library/Calendars
/Users/foo/Library/Calendars/foo.caldav/foo.calendar/Info.plist
/Users/foo/Library/Calendars/foo.calendar.caldav/foo.calendar.calendar/Info.plist
/Users/foo/Library/Calendars/foo.calendar.caldav/Info.plist
/Users/foo/Library/Calendars/foo.calendar.caldav/Info.plist
/Users/foo/Library/Calendars/CalendarCache
然后...
sed -i '' 's/wrongtext/righttext/g’
有任何想法吗?
答案1
应该这样做:
grep -rlZ "wrongtext" ~/Library/Calendars | xargs -0 sed -i '' 's/wrongtext/righttext/g'
我添加了-Z
参数以grep
在每个文件名后添加零字节而不是换行符。因此该命令也适用于奇怪的文件名。xargs
然后读取由零字节分隔的输入-0
并调用sed
命令。
答案2
弄清楚了。它不喜欢-O
grep -rlZ "wrongtext" ~/Library/Calendars | xargs sed -i '' 's/wrongtext/righttext/g'