Paul M. wrote in post #1089118:
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?
data.txt:
Testing
Hello worldno no no no no no
Date:1 2 3 4 5 6 7
Date:a b c d e f g
my_prog.rb:
fname = ARGV[0]
start_column = 3
end_column = 5
target_range = (start_column-1)…(end_column-1)
IO.foreach(fname) do |line|
if line.match(/Date:</strong>/)
pieces = line.split(" ")
puts pieces[target_range].join(":")
end
end
–output:–
$ ruby my_prog.rb data.txt
3:4:5
c:d:e
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.
Nokogiri provides myriad ways to locate any text in an html file.
For instance:
require ‘nokogiri’
fname = ‘data.txt’
@doc = Nokogiri::XML(File.open(fname))
my_xpath ="//strong[text()=‘Date:’]/following-sibling::div[1]"
@doc.xpath(my_xpath).each do |div|
puts div.text.split(" “)[2…4].join(”:")
end
–output:–
$ ruby my_prog.rb
3:4:5
c:d:e
I recognize that there are
things that a
scripting language can’t do - like interacting with a database.
What scripting language is unable to interact with a database?
mysql> use mydb;
mysql> select * from people;
±—±------±------+
| id | name | info |
±—±------±------+
| 1 | Jane | 3 4 5 |
| 2 | John | a b c |
±—±------±------+
require ‘mysql2’
begin
client = Mysql2::Client.new(:host => “localhost”, :username => “root”)
client.query(“USE mydb”)
results = client.query(“SELECT * FROM people”)
results.each do |row|
puts “#{row[‘id’]} #{row[‘name’]} #{row[‘info’]}”
end
client.query(“INSERT INTO people(name, info) VALUES(‘Jeff’, ‘7 8 9’)”)
results = client.query(“SELECT * FROM people”)
results.each do |row|
puts “#{row[‘id’]} #{row[‘name’]} #{row[‘info’]}”
end
rescue Mysql2::Error => e
puts e.errno
puts e.error
ensure
client.close if client
end
–output:–
1 Jane 3 4 5
2 John a b c
1 Jane 3 4 5
2 John a b c
3 Jeff 7 8 9
mysql> select * from people;
±—±------±------+
| id | name | info |
±—±------±------+
| 1 | Jane | 3 4 5 |
| 2 | John | a b c |
| 3 | Jeff | 7 8 9 |
±—±------±------+