我需要替换一个充满文本文件的文件夹中的文本。我该怎么办?
谢谢!
tell application "Finder"
set l to files of desktop as alias list
end tell
repeat with f in l
set input to read f as «class utf8»
set text item delimiters to "search"
set ti to text items of input
set text item delimiters to "replace"
set b to open for access f with write permission
set eof b to 0
write (ti as text) to b as «class utf8»
close access b
end repeat
我大多只是在 TextMate 或 Ruby 脚本的项目中使用 find ,如下所示:
Dir["#{ENV['HOME']}/Desktop/*"].each do |f|
input = File.read(f).gsub(/search/, "replace")
File.open(f, "w") { |f| f.print(input) }
end
你也可以使用 sed:
sed -i '' 's/search/replace/g' ~/Desktop/*