Reading a number of files and and searching for text

Discussion in 'Ruby' started by Paul Donaghy, Apr 25, 2008.

  1. Paul Donaghy

    Paul Donaghy Guest

    Hi Guys,

    I have a number of results files inside a /results folder such as

    /results/results1.txt
    /results/results2.txt
    ...
    ... and so on.

    Inside each of these files are messages either
    This feature Passed
    This feature Failed

    What I want to do is read all of these files /results/results* and pick
    out the messages that say FAILED and store them in a variable or
    whatever so I can print them to screen.

    Any you guys any bright "easy" ideas?

    Help much appreciated.Thanks.
    --
    Posted via http://www.ruby-forum.com/.
     
    Paul Donaghy, Apr 25, 2008
    #1
    1. Advertising

  2. On 25 Apr 2008, at 10:28, Paul Donaghy wrote:
    > Hi Guys,
    >
    > I have a number of results files inside a /results folder such as
    >
    > /results/results1.txt
    > /results/results2.txt
    > ...
    > ... and so on.
    >
    > Inside each of these files are messages either
    > This feature Passed
    > This feature Failed
    >
    > What I want to do is read all of these files /results/results* and
    > pick
    > out the messages that say FAILED and store them in a variable or
    > whatever so I can print them to screen.
    >
    > Any you guys any bright "easy" ideas?
    >
    > Help much appreciated.Thanks.
    > --
    > Posted via http://www.ruby-forum.com/.



    If really that's all you want to do then I'd just use grep rather than
    Ruby:

    grep FAILED results/*.txt

    Alex Gutteridge

    Department of Biochemistry
    University of Cambridge
     
    Alex Gutteridge, Apr 25, 2008
    #2
    1. Advertising

  3. Paul Donaghy

    7stud -- Guest

    Paul Donaghy wrote:
    > Hi Guys,
    >
    > I have a number of results files inside a /results folder such as
    >
    > /results/results1.txt
    > /results/results2.txt
    > ...
    > ... and so on.
    >
    > Inside each of these files are messages either
    > This feature Passed
    > This feature Failed
    >
    > What I want to do is read all of these files /results/results* and pick
    > out the messages that say FAILED and store them in a variable or
    > whatever so I can print them to screen.
    >
    > Any you guys any bright "easy" ideas?
    >
    > Help much appreciated.Thanks.



    Dir.glob('/results/results*.txt') do |fname|
    IO.foreach(fname) do |line|
    if line.include?("Passed")
    puts line
    end
    end
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Apr 26, 2008
    #3
    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. hivie

    Searching text files

    hivie, Jul 8, 2003, in forum: C++
    Replies:
    3
    Views:
    535
    John Harrison
    Jul 8, 2003
  2. Rene Aguirre

    Searching text in Excel, Word files

    Rene Aguirre, Mar 3, 2004, in forum: Python
    Replies:
    0
    Views:
    424
    Rene Aguirre
    Mar 3, 2004
  3. Replies:
    4
    Views:
    974
    M.E.Farmer
    Feb 13, 2005
  4. Matthew Crema

    Reading a large number of text files into an array

    Matthew Crema, Apr 27, 2005, in forum: C Programming
    Replies:
    4
    Views:
    717
    Matthew Crema
    Apr 27, 2005
  5. stumblng.tumblr
    Replies:
    1
    Views:
    217
    stumblng.tumblr
    Feb 4, 2008
Loading...

Share This Page