JosephPecoraro-rr 1.0.3 → 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.
Files changed (4) hide show
  1. data/README.txt +10 -0
  2. data/bin/rr +15 -2
  3. data/lib/rr.rb +1 -1
  4. metadata +1 -1
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/bin/rr CHANGED
@@ -10,6 +10,7 @@ line_processing = false
10
10
  case_sensitive = false
11
11
  global_replace = true
12
12
  modify_original = false
13
+ only_match = false
13
14
 
14
15
  # Usage Message to print
15
16
  $program_name = $0.split(/\//).last
@@ -28,6 +29,7 @@ options:
28
29
  --case or -c makes the regular expression case sensitive (not default)
29
30
  --global or -g process all occurrences in the text (default)
30
31
  --modify or -m changes will directly modify the original file (not default)
32
+ --only or -o print out only what matches the regex
31
33
 
32
34
  negated options are done by adding 'not' or 'n' in switches like so:
33
35
  --notline or -nl
@@ -52,6 +54,11 @@ def err(msg)
52
54
  exit 1
53
55
  end
54
56
 
57
+ # Print out only the matching portion
58
+ def only(file)
59
+
60
+ end
61
+
55
62
  # Possible Switches
56
63
  ARGV.each_index do |i|
57
64
  arg = ARGV[i]
@@ -65,6 +72,8 @@ ARGV.each_index do |i|
65
72
  when "--notglobal", "-ng" then global_replace = false
66
73
  when "--modify" , "-m" then modify_original = true
67
74
  when "--notmodify", "-nm" then modify_original = false
75
+ when "--only" , "-o" then only_match = true
76
+ when "--notonly" , "-no" then only_match = false
68
77
  else err("illegal option #{arg}")
69
78
  end
70
79
  end
@@ -158,9 +167,13 @@ first_filename.upto(ARGV.size-1) do |i|
158
167
  # 2. Read text as 1 big sting (memory intensive) or Line by Line
159
168
  # 3. Run the Find/Replace globally or non-globally
160
169
  # 4. Print the result to a stream
161
- if line_processing and global_replace then
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
162
175
  File.new(filename).readlines.each { |line| stream.puts line.gsub(find,replace) }
163
- elsif line_processing and !global_replace then
176
+ elsif line_processing and !global_replace
164
177
  File.new(filename).readlines.each { |line| stream.puts line.sub(find,replace) }
165
178
  elsif !line_processing and global_replace
166
179
  stream.puts File.new(filename).read.gsub(find,replace)
data/lib/rr.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Rr
2
- VERSION = '1.0.2'
2
+ VERSION = '1.0.4'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: JosephPecoraro-rr
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Pecoraro