Since I keep forgetting it, I'm posting it: Ctrl+L replaces the clear command when you're using Cygwin.
http://hafizpariabi.blogspot.com/2008/02/no-clear-command-in-cygwin-default.html for more (including alternatives like installing ncurses).
Blog descriptions, like blog names, are usually pretty terrible. This is no exception.
Tuesday, March 8, 2011
Monday, March 7, 2011
Ruby in Practice: Regular expressions
Helpful Ruby regular expression cheat sheet
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm
Manipulating file contents with regular expressions in Ruby
http://snippets.dzone.com/posts/show/2595
I don't often need to work with regular expressions -- and when I do I usually use regex search and replace in Eclipse or Notepad++. Today, however, I had a requirement that was harder to address with search and replace. A great opportunity to try my new Ruby skills.
I needed to take something like this:
...
<outer>
<message-driven id="GreenPepper_sub_1" initialState="running" />
<message-driven id="Habanero_sub_1" initialState="running" />
<message-driven id="Avacado_sub_1" initialState="running" />
...
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm
Manipulating file contents with regular expressions in Ruby
http://snippets.dzone.com/posts/show/2595
I don't often need to work with regular expressions -- and when I do I usually use regex search and replace in Eclipse or Notepad++. Today, however, I had a requirement that was harder to address with search and replace. A great opportunity to try my new Ruby skills.
I needed to take something like this:
...
<outer>
<message-driven id="GreenPepper_sub_1" initialState="running" />
<message-driven id="Habanero_sub_1" initialState="running" />
<message-driven id="Avacado_sub_1" initialState="running" />
...
and replace it with this:
...
<outer>
<message-driven id="GreenPepper_sub_1" initialState="running" />
<message-driven id="GreenPepper_sub_2" initialState="running" />
<message-driven id="GreenPepper_sub_3" initialState="running" />
<message-driven id="Habanero_sub_1" initialState="running" />
<message-driven id="Habanero_sub_2" initialState="running" />
<message-driven id="Habanero_sub_3" initialState="running" />
<message-driven id="Avacado_sub_1" initialState="running" />
<message-driven id="Avacado_sub_2" initialState="running" />
<message-driven id="Avacado_sub_3" initialState="running" />
..
There were three other differently-formatted files which required a similar change -- in all several hundred lines making manual cut and paste an uninviting task (especially considering the number of typos I was likely to make with so many changes). Basically, take a line with "_sub_1" and add a few duplicate lines afterward replacing "_sub_<n>"with "_sub_<n>" where <n> is 2 or 3.
As with most one-off utility scripts I didn't want to spend two hours doing one hour's worth of work, so I didn't super-optimize the script, but it worked great. You can see that some of the files had lines with "channel 1" which required a similar change. This one script worked great for all files:
# script to add extra lines to rib-*-adapters-resources.properties
# and rib-*-adapters.xml
# for multi-channel
def replace_in_file(filename="D://rib//file4.txt", match="tafr_1")
File.open(filename, "r") {|f| lines = f.readlines
lines.each {|line|
puts line
if line =~ /#{match}/
replace line, match
end
}
}
end
def replace(line, match)
(2..3).each {|i|
tempLine = line.to_s.gsub(match, match[0..match.index("_1")]+"#{i}")
tempLine = tempLine.to_s.gsub("channel 1", "channel #{i}")
puts tempLine
}
end
Subscribe to:
Posts (Atom)