regex_replace 1.0.2 → 1.0.4
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/README.txt +10 -0
- data/Rakefile +0 -2
- data/bin/rr +15 -5
- data/lib/rr.rb +1 -1
- metadata +11 -6
data/README.txt
CHANGED
@@ -13,6 +13,7 @@ to appear in the replacement text.
|
|
13
13
|
usage: rr [options] find replace [filenames]
|
14
14
|
rr [options] s/find/replace/ [filenames]
|
15
15
|
rr [options] find
|
16
|
+
|
16
17
|
find - a regular expression to be run on the entire file as one string
|
17
18
|
the final usage defaults the replacement to the empty string
|
18
19
|
replace - replacement text, \1-\9 and metachars (\n, etc.) are allowed
|
@@ -23,6 +24,7 @@ to appear in the replacement text.
|
|
23
24
|
--case or -c makes the regular expression case sensitive (not default)
|
24
25
|
--global or -g process all occurrences in the text (default)
|
25
26
|
--modify or -m changes will directly modify the original file (not default)
|
27
|
+
--only or -o print out only what matches the regex
|
26
28
|
|
27
29
|
negated options are done by adding 'not' or 'n' in switches like so:
|
28
30
|
--notline or -nl
|
@@ -30,6 +32,14 @@ to appear in the replacement text.
|
|
30
32
|
special note:
|
31
33
|
When using bash, if you want backslashes in the replace portion make sure
|
32
34
|
to use the multiple argument usage with single quotes for the replacement.
|
35
|
+
|
36
|
+
example usage:
|
37
|
+
Replace all a's with e's
|
38
|
+
rr s/a/e/ file
|
39
|
+
rr a e file
|
40
|
+
|
41
|
+
Doubles the last character on each line and doubles the newline.
|
42
|
+
rr "(.)\n" "\1\1\n\n" file
|
33
43
|
|
34
44
|
|
35
45
|
== SYNOPSIS:
|
data/Rakefile
CHANGED
data/bin/rr
CHANGED
@@ -1,7 +1,4 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
# Start Date: Sunday December 2, 2007 (v0.9)
|
3
|
-
# Most Recent Update: Sunday June 29, 2008 (v1.1)
|
4
|
-
# Current Version: 1.1
|
5
2
|
# Author: Joseph Pecoraro
|
6
3
|
# Contact: joepeck02@gmail.com
|
7
4
|
# Decription: Default behavior is a multi-line search and replace utility that
|
@@ -13,6 +10,7 @@ line_processing = false
|
|
13
10
|
case_sensitive = false
|
14
11
|
global_replace = true
|
15
12
|
modify_original = false
|
13
|
+
only_match = false
|
16
14
|
|
17
15
|
# Usage Message to print
|
18
16
|
$program_name = $0.split(/\//).last
|
@@ -31,6 +29,7 @@ options:
|
|
31
29
|
--case or -c makes the regular expression case sensitive (not default)
|
32
30
|
--global or -g process all occurrences in the text (default)
|
33
31
|
--modify or -m changes will directly modify the original file (not default)
|
32
|
+
--only or -o print out only what matches the regex
|
34
33
|
|
35
34
|
negated options are done by adding 'not' or 'n' in switches like so:
|
36
35
|
--notline or -nl
|
@@ -55,6 +54,11 @@ def err(msg)
|
|
55
54
|
exit 1
|
56
55
|
end
|
57
56
|
|
57
|
+
# Print out only the matching portion
|
58
|
+
def only(file)
|
59
|
+
|
60
|
+
end
|
61
|
+
|
58
62
|
# Possible Switches
|
59
63
|
ARGV.each_index do |i|
|
60
64
|
arg = ARGV[i]
|
@@ -68,6 +72,8 @@ ARGV.each_index do |i|
|
|
68
72
|
when "--notglobal", "-ng" then global_replace = false
|
69
73
|
when "--modify" , "-m" then modify_original = true
|
70
74
|
when "--notmodify", "-nm" then modify_original = false
|
75
|
+
when "--only" , "-o" then only_match = true
|
76
|
+
when "--notonly" , "-no" then only_match = false
|
71
77
|
else err("illegal option #{arg}")
|
72
78
|
end
|
73
79
|
end
|
@@ -161,9 +167,13 @@ first_filename.upto(ARGV.size-1) do |i|
|
|
161
167
|
# 2. Read text as 1 big sting (memory intensive) or Line by Line
|
162
168
|
# 3. Run the Find/Replace globally or non-globally
|
163
169
|
# 4. Print the result to a stream
|
164
|
-
if
|
170
|
+
if only_match && replace == ''
|
171
|
+
stream.puts File.new(filename).read.scan(find).join("\n")
|
172
|
+
elsif only_match
|
173
|
+
File.new(filename).read.scan(find) { stream.puts $&.gsub(find,replace) }
|
174
|
+
elsif line_processing and global_replace
|
165
175
|
File.new(filename).readlines.each { |line| stream.puts line.gsub(find,replace) }
|
166
|
-
elsif line_processing and !global_replace
|
176
|
+
elsif line_processing and !global_replace
|
167
177
|
File.new(filename).readlines.each { |line| stream.puts line.sub(find,replace) }
|
168
178
|
elsif !line_processing and global_replace
|
169
179
|
stream.puts File.new(filename).read.gsub(find,replace)
|
data/lib/rr.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: regex_replace
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joseph Pecoraro
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-07-14 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -20,9 +20,12 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 1.
|
23
|
+
version: 1.12.2
|
24
24
|
version:
|
25
|
-
description:
|
25
|
+
description: |-
|
26
|
+
A multi-line search and replace utility that uses Ruby regular expressions
|
27
|
+
for searching and allows back references to captured groups from the pattern
|
28
|
+
to appear in the replacement text.
|
26
29
|
email:
|
27
30
|
- joepeck02@gmail.com
|
28
31
|
executables:
|
@@ -43,6 +46,8 @@ files:
|
|
43
46
|
- test/test_rr.rb
|
44
47
|
has_rdoc: true
|
45
48
|
homepage: http://blog.bogojoker.com
|
49
|
+
licenses: []
|
50
|
+
|
46
51
|
post_install_message:
|
47
52
|
rdoc_options:
|
48
53
|
- --main
|
@@ -64,9 +69,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
69
|
requirements: []
|
65
70
|
|
66
71
|
rubyforge_project: bogojoker
|
67
|
-
rubygems_version: 1.3.
|
72
|
+
rubygems_version: 1.3.3
|
68
73
|
signing_key:
|
69
|
-
specification_version:
|
74
|
+
specification_version: 3
|
70
75
|
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.
|
71
76
|
test_files:
|
72
77
|
- test/test_rr.rb
|