From: sto.mar@...
Date: 2012-12-15T17:37:41+09:00
Subject: Re: Invoking bash from Ruby
Am 14.12.2012 19:15, schrieb Paul Mena:
> I'm a novice at Ruby, although I've been introduced to its syntax by
> virtue of working with Puppet for the past 2 1/2 years. As a sys admin,
> I'm most comfortable working with bash, but I recognize that there are
> things that a
> scripting language can't do - like interacting with a database. So I'm
> writing some Ruby code to extract text out of an HTML file and then
> insert it into a mySQL database. I've been using Nokogiri to get some
> formatted text, but also need to get some text that's easy enough to get
> with a scripting language like bash. Specifically, I need to do this:
>
> grep "Date:" $1 | cut -d' ' -f3-5
>
> Is there an easy way to embed this logic within Ruby code - or, better
> yet - a way to do the same thing in Ruby?
Yes, you can do this easily without using Bash:
pattern = %r{Date:}
File.readlines('testfile.txt').grep(pattern).each do |line|
p line.split(' ')[2..4]
end
Output:
["2", "3", "4"]
["b", "c", "d"]
where:
$ cat testfile.txt
Date: 1 2 3 4 5
line
another line without Date
Date: a b c d e
yet another line
--