I want to write command line Ruby program whose arguments will be a
list of words to be found within all HTML documents of the current
directory. The output of the program will be a text file called
results.txt and will consist of a list of all HTML documents in which
one of the words appeared. Have the program create a subdirectory
called FINAL ,of the current directory and save results.txt there.
Finally, it will make duplicate copies of each of the files listed in
results.txt within the FINAL directory.
Any help would be greatly appreciated
George
George wrote:
I want to write command line Ruby program whose arguments will be a
list of words to be found within all HTML documents of the current
directory. The output of the program will be a text file called
results.txt and will consist of a list of all HTML documents in which
one of the words appeared. Have the program create a subdirectory
called FINAL ,of the current directory and save results.txt there.
Finally, it will make duplicate copies of each of the files listed in
results.txt within the FINAL directory.
Any help would be greatly appreciated
It’s awfully late in the semester for your professor to be making new
programming assignments, isn’t it?
system(%q!sh “list=‘word1 word2 …’; mkdir -p FINAL; for a in list; do for f in *.html; do grep $a $f >/dev/null && echo $f >> FINAL/results.txt && cp $f FINAL; done; done”!)
I think
···
George (geoflorida2000@yahoo.com) wrote:
I want to write command line Ruby program whose arguments will be a
list of words to be found within all HTML documents of the current
directory. The output of the program will be a text file called
results.txt and will consist of a list of all HTML documents in which
one of the words appeared. Have the program create a subdirectory
called FINAL ,of the current directory and save results.txt there.
Finally, it will make duplicate copies of each of the files listed in
results.txt within the FINAL directory.
Any help would be greatly appreciated
George
–
Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
something like this could work
#lil code, missing excepetion handling, one time runnable
Dir.mkdir(“./FINAL”)
File.open (“FINAL/result.txt”, “w+”) { |out|
Dir[“*.html”].each { |file|
IO.foreach(file) { |f|
out.puts file if f =~ /#{ARGV.join(“|”)}/
}
}
}
IO.foreach “FINAL/result.txt” { |line|
system(“mv #{line.chomp} FINAL/”)
}
#should handle this to be xplatform
Is nice whane you don’t follow a Ruby programing course and get the
homework anyway
···
On 10 Dec 2002 12:09:13 -0800, geoflorida2000@yahoo.com (George) wrote:
I want to write command line Ruby program whose arguments will be a
list of words to be found within all HTML documents of the current
directory. The output of the program will be a text file called
results.txt and will consist of a list of all HTML documents in which
one of the words appeared. Have the program create a subdirectory
called FINAL ,of the current directory and save results.txt there.
Finally, it will make duplicate copies of each of the files listed in
results.txt within the FINAL directory.
Any help would be greatly appreciated
George
That’s cheating.
Tim Bates
···
On Wed, 11 Dec 2002 10:59 am, Eric Hodel wrote:
system(%q!sh “list=‘word1 word2 …’; mkdir -p FINAL; for a in list; do for
f in *.html; do grep $a $f >/dev/null && echo $f >> FINAL/results.txt && cp
$f FINAL; done; done”!)I think
–
tim@bates.id.au
On 10 Dec 2002 12:09:13 -0800, geoflorida2000@yahoo.com (George)
I want to write command line Ruby program whose arguments will be a
list of words to be found within all HTML documents of the current
directory. The output of the program will be a text file called
results.txt and will consist of a list of all HTML documents in which
one of the words appeared. Have the program create a subdirectory
called FINAL ,of the current directory and save results.txt there.
Finally, it will make duplicate copies of each of the files listed in
results.txt within the FINAL directory.
Any help would be greatly appreciated
George
George,
Here’s my try at it; AKAIK it’s platform independent. Note I included regex
support.
[Calling the script multiple times with different arguments will screw up the
results-- FINAL should be deleted after every use-- this can be added to the
script.]
···
#!/usr/bin/ruby -w
require ‘ftools’
DIR = ‘FINAL’
RESULTS = File::join DIR, ‘results.txt’
class PageHarvest
def initialize words
@matches =
Dir[‘*.htm?’].each do |file|
data = File::read file # for 1.6.7: File::open(file){ |f| f.read }
words.each do |word|
if not @matches.include?(file)
# change below to: data.include?(word)
# if you don’t want regexes
@matches << file if data =~ /#{word}/
end
end
end
finalize unless @matches.empty?
end
def finalize
Dir::mkdir(DIR) unless File::directory?(DIR)
File::open(RESULTS, ‘w’) do |results|
@matches.each do |match|
results.puts match
File::copy match, File::join(DIR, match)
end
end
end
end
runtime…
PageHarvest.new(ARGV) if FILE == $0
(and yes, I’m aware it might be a bit much to write a Class for such a simple
thing-- but how could I let a name like ‘PageHarvest’ go to waste?
Hope this helps,
– Bruce
–
Bruce R. Williams :: [iusris/#ruby-lang] :: http://www.codedbliss.com
‘It does not require a majority to prevail, but rather an irate,
tireless minority keen to set brush fires in people’s minds.’
– Samuel Adams
George,
Here’s my try at it; AKAIK it’s platform independent. Note I included regex
support.
what does AKAIK means ? (jargon… I’ll never gork it )
[Calling the script multiple times with different arguments will screw up the
results-- FINAL should be deleted after every use-- this can be added to the
script.]
err, I didnt’ get one thing , where do you move the files ?
···
On Wed, 11 Dec 2002 22:44:52 +0900, Bruce Williams bruce@codedbliss.com wrote:
gabriele renzi wrote:
···
On Wed, 11 Dec 2002 22:44:52 +0900, Bruce Williams > bruce@codedbliss.com wrote:
George,
Here’s my try at it; AKAIK it’s platform independent. Note I included regex
support.what does AKAIK means ? (jargon… I’ll never gork it )
I think it was supposed to be AFAIK, which is “As Far As I Know”
Others you may see:
IIRC - if I recall correctly
AFAICT - as far as I can tell
Regards,
Dan
Others you may see:
IIRC - if I recall correctly
AFAICT - as far as I can tellRegards,
Dan
One that threw me off for a while :
FWIW => “For what it’s worth”
and if you’re on Slashdot a lot :
IANAL => I am not a lawyer
usually followed up by some very naive commentary
Jason