Reputation: 6292
I have a pseudo-code method that works like this:
def my_method(file)
while(line = file.gets)
case line
when /^TEXT (.*)/
puts line + <the text captured in the parenthesis of the regex>
else
.....
end
end
end
Is there any way to do this?
EDIT:
The sample string is like:
TEXT a sample text
I want to have "a sample text" captured by the regex. I know this is not the proper way to do this, but this is just a demonstration, i.e. "YYYY-MM-DD format date in shell script" to figure out how to get the date in whatever format you want.
Yesterday's date can be found as:
date -d '1 day ago' +'%Y/%m/%d'
from "How To Get Yesterday’s Date using BASH Shell Scripting".
Replace the /
with -
or _
and then pass them in to the Ruby statement.
Edit: Vote for the other guy. Their answer actually has code.
Upvotes: 1
Views: 163
Reputation: 237010
What you are looking for is puts "line#{$1}"
. The pseudo-globals $1
, $2
, $3
, etc. refer to capture groups of the last Regexp match. (And $~
refers to the MatchData itself, if you'd like to work with that.)
Upvotes: 4