-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfar
More file actions
executable file
·48 lines (42 loc) · 1.1 KB
/
far
File metadata and controls
executable file
·48 lines (42 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env ruby
#
# Super simple ruby script that can make it easier to run a command line
# recursive find/replace on a directory. Because I was fed up of googling
# the relevant grep/sed command
require 'getoptlong'
opts = GetoptLong.new(
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--version', '-v', GetoptLong::NO_ARGUMENT ],
[ '--find', '-f', GetoptLong::REQUIRED_ARGUMENT ],
[ '--replace', '-r', GetoptLong::REQUIRED_ARGUMENT ]
)
find = ''
replace = ''
opts.each do |opt, arg|
case opt
when '--help'
abort 'Example usage: \'far --find foo --replace bar\''
when '--version'
abort 'Find and Replace version 1.0'
when '--find'
find = arg
when '--replace'
replace = arg
end
end
if (find == '')
puts "You didn't specify both a --find value and a --replace value"
else
puts "Finding #{find}, replacing with #{replace}"
Dir['**/*'].each do |path|
if File.file?(path) and !File.binary?(path)
puts "Processing file #{path}"
begin
text = File.read(path).gsub(find, replace)
File.open(path, "w").write(text)
rescue
puts "Not a text file"
end
end
end
end