file grep

Discussion in 'Ruby' started by Abhijeet Dharmapurikar, Aug 16, 2007.

  1. I have a file that has a list of filenames- filenames.txt
    I have another file that has list of all the files in the system with
    their complete filepaths - allfilespath.txt

    I want to print the fullpaths of all the files listed in filenames.txt
    In shell scripting I would have done somethign like this

    for name in filenames.txt
    do
    grep $name allfilespath.txt
    if [ $? == 1 ]
    then
    echo $output_from_grep
    fi
    done


    How do I do this in ruby ? If possible (again if possible) explain what
    the code does. If not I will google and update what each line does.
    --
    Posted via http://www.ruby-forum.com/.
    Abhijeet Dharmapurikar, Aug 16, 2007
    #1
    1. Advertising

  2. Thanks Felix I just had to change one line
    < paths.find_all{|path| File.basename == filename}.each do |match|
    > paths.find_all{|path| File.basename(path) == filename}.each do |match|


    but it worked like a charm.

    Regards,
    Abhijeet



    Felix Windt wrote:
    >>
    >> done
    >>
    >>
    >> How do I do this in ruby ? If possible (again if possible)
    >> explain what the code does. If not I will google and update
    >> what each line does.
    >> --
    >> Posted via http://www.ruby-forum.com/.
    >>

    >
    >
    > Disclaimer: Reading the complete files in to arrays like this may cost a
    > bunch of memory depending on file size
    >
    > # read in an array that contains one element per line from the list of
    > filesnames
    > filenames = File.new("filenames.txt").readlines
    > # read in an array that contains one element per line from the list of
    > full
    > filepaths
    > paths = File.new("allfilespath.txt").readlines
    > # cycle through all filenames
    > filenames.each do |filename|
    > # File.basename with one argument returns the last element of the
    > filepath, so the filename in a path
    > # the find_all method from the Enumerable mixin passes each element of
    > the
    > collection to the block, and returns an array of those where the block
    > evaluates to true
    > paths.find_all{|path| File.basename == filename}.each do |match|
    > # cycle through the matches, and print them
    > puts "#{filename} matches #{match}"
    > end
    > end
    >
    >
    > HTH,
    >
    > Felix


    --
    Posted via http://www.ruby-forum.com/.
    Abhijeet Dharmapurikar, Aug 17, 2007
    #2
    1. Advertising

  3. 2007/8/16, Felix Windt <>:
    > > -----Original Message-----
    > > From:
    > > [mailto:] On Behalf Of Abhijeet Dharmapurikar
    > > Sent: Thursday, August 16, 2007 12:51 PM
    > > To: ruby-talk ML
    > > Subject: file grep
    > >
    > > I have a file that has a list of filenames- filenames.txt I
    > > have another file that has list of all the files in the
    > > system with their complete filepaths - allfilespath.txt
    > >
    > > I want to print the fullpaths of all the files listed in
    > > filenames.txt In shell scripting I would have done somethign like this
    > >
    > > for name in filenames.txt
    > > do
    > > grep $name allfilespath.txt
    > > if [ $? == 1 ]
    > > then
    > > echo $output_from_grep
    > > fi
    > > done
    > >
    > >
    > > How do I do this in ruby ? If possible (again if possible)
    > > explain what the code does. If not I will google and update
    > > what each line does.

    >
    > Disclaimer: Reading the complete files in to arrays like this may cost a
    > bunch of memory depending on file size


    Here's another solution that avoids reading the large file into mem
    but it does not write out names ordered:

    require 'set'
    names = Set.new(File.readlines("filenames.txt").each {|s| s.chomp!})

    File.open("allfilespath.txt") do |io|
    io.each do |line|
    line.chomp!
    puts line if names.includes? File.basename(line)
    end
    end

    Kind regards

    robert
    Robert Klemme, Aug 17, 2007
    #3
  4. Abhijeet Dharmapurikar

    Kaldrenon Guest

    On Aug 17, 9:47 am, "Robert Klemme" <>
    wrote:
    > Here's another solution that avoids reading the large file into mem
    > but it does not write out names ordered:
    >
    > require 'set'
    > names = Set.new(File.readlines("filenames.txt").each {|s| s.chomp!})
    >
    > File.open("allfilespath.txt") do |io|
    > io.each do |line|
    > line.chomp!
    > puts line if names.includes? File.basename(line)
    > end
    > end
    >
    > Kind regards
    >
    > robert


    Is there a reason to do it this way instead of the slightly more
    compact way below?

    File.open("allfilespath.txt").each_line do |line|
    line.chomp!
    puts line if names.includes? File.basename(line)
    end

    Is there a difference in behavior, or only in appearance?
    Kaldrenon, Aug 17, 2007
    #4
  5. On 17.08.2007 16:30, Kaldrenon wrote:
    > On Aug 17, 9:47 am, "Robert Klemme" <>
    > wrote:
    >> Here's another solution that avoids reading the large file into mem
    >> but it does not write out names ordered:
    >>
    >> require 'set'
    >> names = Set.new(File.readlines("filenames.txt").each {|s| s.chomp!})
    >>
    >> File.open("allfilespath.txt") do |io|
    >> io.each do |line|
    >> line.chomp!
    >> puts line if names.includes? File.basename(line)
    >> end
    >> end
    >>
    >> Kind regards
    >>
    >> robert

    >
    > Is there a reason to do it this way instead of the slightly more
    > compact way below?
    >
    > File.open("allfilespath.txt").each_line do |line|
    > line.chomp!
    > puts line if names.includes? File.basename(line)
    > end
    >
    > Is there a difference in behavior, or only in appearance?


    The more compact version does not properly close the file. Although you
    can fix that by doing

    File.open("allfilespath.txt").each_line do |line|
    line.chomp!
    puts line if names.includes? File.basename(line)
    end.close
    ^^^^^

    there is still a difference: my version will close the file regardless
    how the block is left (i.e. even in case of an exception) while the
    fixed compact version does not close the file if the block is left via
    an exception.

    Kind regards

    robert
    Robert Klemme, Aug 17, 2007
    #5
  6. Abhijeet Dharmapurikar

    Kaldrenon Guest

    On Aug 17, 1:30 pm, Robert Klemme <> wrote:
    > > Is there a reason to do it this way instead of the slightly more
    > > compact way below?

    >
    > > File.open("allfilespath.txt").each_line do |line|
    > > line.chomp!
    > > puts line if names.includes? File.basename(line)
    > > end

    >
    > > Is there a difference in behavior, or only in appearance?

    >
    > The more compact version does not properly close the file. Although you
    > can fix that by doing
    >
    > File.open("allfilespath.txt").each_line do |line|
    > line.chomp!
    > puts line if names.includes? File.basename(line)
    > end.close
    > ^^^^^
    >
    > there is still a difference: my version will close the file regardless
    > how the block is left (i.e. even in case of an exception) while the
    > fixed compact version does not close the file if the block is left via
    > an exception.


    That's good to know, glad I asked. Thanks!
    Kaldrenon, Aug 17, 2007
    #6
  7. This can be solved with a begin rescue statement though:

    begin
    # File manipulation
    rescue => e
    file.close unless file.nil?
    end

    Kaldrenon wrote:
    > On Aug 17, 1:30 pm, Robert Klemme <> wrote:
    >> The more compact version does not properly close the file. Although you
    >> fixed compact version does not close the file if the block is left via
    >> an exception.

    >
    > That's good to know, glad I asked. Thanks!


    --
    Posted via http://www.ruby-forum.com/.
    Alexander Mcconaughey, May 18, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. danpres2k
    Replies:
    3
    Views:
    7,456
    danpres2k
    Aug 25, 2003
  2. qanda
    Replies:
    9
    Views:
    156
    qanda
    Sep 11, 2003
  3. qanda
    Replies:
    1
    Views:
    107
    Tassilo v. Parseval
    Sep 15, 2003
  4. J M
    Replies:
    2
    Views:
    147
    Darren Dunham
    Jan 26, 2004
  5. Ben Nguyen
    Replies:
    4
    Views:
    115
    Anno Siegel
    May 31, 2004
Loading...

Share This Page