regex_replace 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +5 -0
- data/Manifest.txt +7 -0
- data/README.txt +73 -0
- data/Rakefile +12 -0
- data/bin/rr +173 -0
- data/lib/rr.rb +3 -0
- data/test/test_rr.rb +0 -0
- metadata +71 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
= rr
|
2
|
+
|
3
|
+
* http://blog.bogojoker.com
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
A multi-line search and replace utility that uses Ruby regular expressions
|
8
|
+
for searching and allows back references to captured groups from the pattern
|
9
|
+
to appear in the replacement text.
|
10
|
+
|
11
|
+
== FEATURES/PROBLEMS:
|
12
|
+
|
13
|
+
<pre>usage: rr [options] find replace [filenames]
|
14
|
+
rr [options] s/find/replace/ [filenames]
|
15
|
+
find - a regular expression to be run on the entire file as one string
|
16
|
+
replace - replacement text, \1-\9 and metachars (\n, etc.) are allowed
|
17
|
+
filenames - names of the input files to be parsed, if blank uses STDIN
|
18
|
+
|
19
|
+
options:
|
20
|
+
--line or -l process line by line instead of all at once (not default)
|
21
|
+
--case or -c makes the regular expression case sensitive (not default)
|
22
|
+
--global or -g process all occurrences in the text (default)
|
23
|
+
--modify or -m changes will directly modify the original file (not default)
|
24
|
+
|
25
|
+
negated options are done by adding 'not' or 'n' in switches like so:
|
26
|
+
--notline or -nl
|
27
|
+
|
28
|
+
special note:
|
29
|
+
When using bash, if you want backslashes in the replace portion make sure
|
30
|
+
to use the multiple argument usage with single quotes for the replacement.
|
31
|
+
</pre>
|
32
|
+
|
33
|
+
== SYNOPSIS:
|
34
|
+
|
35
|
+
Replace all a's with e's
|
36
|
+
rr s/a/e/ file
|
37
|
+
rr a e file
|
38
|
+
|
39
|
+
Doubles the last character on each line and doubles the newline.
|
40
|
+
rr "(.)\n" "\1\1\n\n" file
|
41
|
+
|
42
|
+
== REQUIREMENTS:
|
43
|
+
|
44
|
+
* Ruby 1.8.6 or greater
|
45
|
+
|
46
|
+
== INSTALL:
|
47
|
+
|
48
|
+
* sudo gem install rr
|
49
|
+
|
50
|
+
== LICENSE:
|
51
|
+
|
52
|
+
(The MIT License)
|
53
|
+
|
54
|
+
Copyright (c) 2008 Joseph Pecoraro
|
55
|
+
|
56
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
57
|
+
a copy of this software and associated documentation files (the
|
58
|
+
'Software'), to deal in the Software without restriction, including
|
59
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
60
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
61
|
+
permit persons to whom the Software is furnished to do so, subject to
|
62
|
+
the following conditions:
|
63
|
+
|
64
|
+
The above copyright notice and this permission notice shall be
|
65
|
+
included in all copies or substantial portions of the Software.
|
66
|
+
|
67
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
68
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
69
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
70
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
71
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
72
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
73
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/bin/rr
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Start Date: Sunday December 2, 2007 (v0.9)
|
3
|
+
# Most Recent Update: Saturday January 26, 2008
|
4
|
+
# Current Version: 1.0
|
5
|
+
# Author: Joseph Pecoraro
|
6
|
+
# Contact: joepeck02@gmail.com
|
7
|
+
# Decription: Default behavior is a multi-line search and replace utility that
|
8
|
+
# uses a regular expression for searching and allows back references to
|
9
|
+
# captured groups from the pattern to appear in the replacement text
|
10
|
+
|
11
|
+
# Global States
|
12
|
+
line_processing = false
|
13
|
+
case_sensitive = false
|
14
|
+
global_replace = true
|
15
|
+
modify_original = false
|
16
|
+
|
17
|
+
# Usage Message to print
|
18
|
+
$program_name = $0.split(/\//).last
|
19
|
+
usage = <<USAGE
|
20
|
+
usage: #{$program_name} [options] find replace [filenames]
|
21
|
+
#{$program_name} [options] s/find/replace/ [filenames]
|
22
|
+
find - a regular expression to be run on the entire file as one string
|
23
|
+
replace - replacement text, \\1-\\9 and metachars (\\n, etc.) are allowed
|
24
|
+
filenames - names of the input files to be parsed, if blank uses STDIN
|
25
|
+
|
26
|
+
options:
|
27
|
+
--line or -l process line by line instead of all at once (not default)
|
28
|
+
--case or -c makes the regular expression case sensitive (not default)
|
29
|
+
--global or -g process all occurrences in the text (default)
|
30
|
+
--modify or -m changes will directly modify the original file (not default)
|
31
|
+
|
32
|
+
negated options are done by adding 'not' or 'n' in switches like so:
|
33
|
+
--notline or -nl
|
34
|
+
|
35
|
+
special note:
|
36
|
+
When using bash, if you want backslashes in the replace portion make sure
|
37
|
+
to use the multiple argument usage with single quotes for the replacement.
|
38
|
+
|
39
|
+
example usage:
|
40
|
+
Replace all a's with e's
|
41
|
+
#{$program_name} s/a/e/ file
|
42
|
+
#{$program_name} a e file
|
43
|
+
|
44
|
+
Doubles the last character on each line and doubles the newline.
|
45
|
+
#{$program_name} "(.)\\n" "\\1\\1\\n\\n" file
|
46
|
+
|
47
|
+
USAGE
|
48
|
+
|
49
|
+
# Print an error message and exit
|
50
|
+
def err(msg)
|
51
|
+
puts "#{$program_name}: #{msg}"
|
52
|
+
exit 1
|
53
|
+
end
|
54
|
+
|
55
|
+
# Possible Switches
|
56
|
+
ARGV.each_index do |i|
|
57
|
+
arg = ARGV[i]
|
58
|
+
if arg[0] == ?- then
|
59
|
+
case arg
|
60
|
+
when "--line" , "-l" then line_processing = true
|
61
|
+
when "--notline" , "-nl" then line_processing = false
|
62
|
+
when "--case" , "-c" then case_sensitive = true
|
63
|
+
when "--notcase" , "-nc" then case_sensitive = false
|
64
|
+
when "--global" , "-g" then global_replace = true
|
65
|
+
when "--notglobal", "-ng" then global_replace = false
|
66
|
+
when "--modify" , "-m" then modify_original = true
|
67
|
+
when "--notmodify", "-nm" then modify_original = false
|
68
|
+
else err("illegal option #{arg}")
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Remove the Switches from ARGV
|
74
|
+
ARGV.delete_if { |elem| elem[0] == ?- }
|
75
|
+
|
76
|
+
# Check if it is quick mode (meaning cmdline argument is s/find/replace/)
|
77
|
+
find_str = find = replace = filename = nil
|
78
|
+
if ARGV.first =~ /^s\/(.*)?\/(.*)?\/(\w*)?$/
|
79
|
+
find_str = $1
|
80
|
+
replace = $2
|
81
|
+
first_filename = 1
|
82
|
+
ARGV << nil if ARGV.size == 1
|
83
|
+
|
84
|
+
# Not Quick Mode, Arguments are seperate on the cmdline
|
85
|
+
elsif ARGV.size >= 2
|
86
|
+
find_str = ARGV[0]
|
87
|
+
replace = ARGV[1].dup
|
88
|
+
first_filename = 2
|
89
|
+
ARGV << nil if ARGV.size == 2
|
90
|
+
|
91
|
+
# User is allowed to wrap the find regex in /'s (this removes them)
|
92
|
+
find_str = find_str[1..(find_str.length-2)] if find_str =~ /^\/.*?\/$/
|
93
|
+
|
94
|
+
# Bad Arguments, show usage
|
95
|
+
else
|
96
|
+
puts usage
|
97
|
+
exit 1
|
98
|
+
end
|
99
|
+
|
100
|
+
# Make find_str into a Regexp object
|
101
|
+
if case_sensitive
|
102
|
+
find = Regexp.new( find_str )
|
103
|
+
else
|
104
|
+
find = Regexp.new( find_str, Regexp::IGNORECASE )
|
105
|
+
end
|
106
|
+
|
107
|
+
# Map metacharacters in the replace portion
|
108
|
+
replace.gsub!(/\\[\\ntrvfbae]/) do |match|
|
109
|
+
case match
|
110
|
+
when "\\\\" then match = "\\" # A backslash
|
111
|
+
when "\\n" then match = "\n" # Newline
|
112
|
+
when "\\t" then match = "\t" # Tab
|
113
|
+
when "\\r" then match = "\r" # Carriage Return
|
114
|
+
when "\\v" then match = "\v" # Vertical Tab
|
115
|
+
when "\\f" then match = "\f" # Formfeed
|
116
|
+
when "\\b" then match = "\b" # Backspace
|
117
|
+
when "\\a" then match = "\a" # Bell
|
118
|
+
when "\\e" then match = "\e" # Escape
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# Loop through all the filenames doing the find/replace
|
123
|
+
first_filename.upto(ARGV.size-1) do |i|
|
124
|
+
|
125
|
+
# Check for Possible File Errors or if the filename is nil make it STDIN
|
126
|
+
filename = ARGV[i]
|
127
|
+
unless filename.nil?
|
128
|
+
if !File.exist? filename
|
129
|
+
err("#{filename}: No such file")
|
130
|
+
elsif File.directory? filename
|
131
|
+
err("#{filename}: This is a directory, not a file.")
|
132
|
+
elsif !File.readable? filename
|
133
|
+
err("#{filename}: File is not readable by this user.")
|
134
|
+
elsif !File.writable? filename
|
135
|
+
err("#{filename}: File is not writable by this user.")
|
136
|
+
end
|
137
|
+
else
|
138
|
+
filename = STDIN.fileno
|
139
|
+
end
|
140
|
+
|
141
|
+
# Setup the stream to print to
|
142
|
+
if modify_original && filename != STDIN.fileno then
|
143
|
+
temp_filename = filename + '.tmp'
|
144
|
+
stream = File.new(temp_filename, File::CREAT|File::TRUNC|File::RDWR, 0644)
|
145
|
+
else
|
146
|
+
stream = STDOUT
|
147
|
+
end
|
148
|
+
|
149
|
+
# Default Behavior (and basicically what they all do)
|
150
|
+
# 1. Open the file
|
151
|
+
# 2. Read text as 1 big sting (memory intensive) or Line by Line
|
152
|
+
# 3. Run the Find/Replace globally or non-globally
|
153
|
+
# 4. Print the result to a stream
|
154
|
+
if line_processing and global_replace then
|
155
|
+
File.new(filename).readlines.each { |line| stream.puts line.gsub(find,replace) }
|
156
|
+
elsif line_processing and !global_replace then
|
157
|
+
File.new(filename).readlines.each { |line| stream.puts line.sub(find,replace) }
|
158
|
+
elsif !line_processing and global_replace
|
159
|
+
stream.puts File.new(filename).read.gsub(find,replace)
|
160
|
+
else
|
161
|
+
stream.puts File.new(filename).read.sub(find,replace)
|
162
|
+
end
|
163
|
+
|
164
|
+
# If the stream was a temp file then clean up
|
165
|
+
if modify_original && filename != STDIN.fileno then
|
166
|
+
stream.close
|
167
|
+
File.rename(temp_filename, filename)
|
168
|
+
end
|
169
|
+
|
170
|
+
end
|
171
|
+
|
172
|
+
# Successful
|
173
|
+
exit 0
|
data/lib/rr.rb
ADDED
data/test/test_rr.rb
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: regex_replace
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joseph Pecoraro
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-05-31 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.5.3
|
23
|
+
version:
|
24
|
+
description: A multi-line search and replace utility that uses Ruby regular expressions for searching and allows back references to captured groups from the pattern to appear in the replacement text.
|
25
|
+
email:
|
26
|
+
- joepeck02@gmail.com
|
27
|
+
executables:
|
28
|
+
- rr
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- History.txt
|
33
|
+
- Manifest.txt
|
34
|
+
- README.txt
|
35
|
+
files:
|
36
|
+
- History.txt
|
37
|
+
- Manifest.txt
|
38
|
+
- README.txt
|
39
|
+
- Rakefile
|
40
|
+
- bin/rr
|
41
|
+
- lib/rr.rb
|
42
|
+
- test/test_rr.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://blog.bogojoker.com
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --main
|
48
|
+
- README.txt
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project: bogojoker
|
66
|
+
rubygems_version: 1.1.1
|
67
|
+
signing_key:
|
68
|
+
specification_version: 2
|
69
|
+
summary: A multi-line search and replace utility that uses Ruby regular expressions for searching and allows back references to captured groups from the pattern to appear in the replacement text.
|
70
|
+
test_files:
|
71
|
+
- test/test_rr.rb
|