extract consecutive lines of data

Discussion in 'Ruby' started by baptiste Auguié, Nov 24, 2007.

  1. Hi,


    A basic question: how to extract several consecutive lines of numbers
    in a text file (example below) and assign them to some GSL::Matrix in
    Ruby?

    text file:

    > some lines of text ...
    > ....
    > more text
    > below is the data to extract
    >
    > 1 2 3
    > 4 5 6
    > 7 8 9
    > 23 7.3e-8 3.4
    >
    >
    > some more text



    from which i would like to create three vectors, a=[1, 4, 7, 23], b=
    [2, 5, 8, 7.33-8], c=[3, 6, 9, 3.4]. The line numbers could be used
    for indexing if that can help.

    Many thanks,

    baptiste
    baptiste Auguié, Nov 24, 2007
    #1
    1. Advertising

  2. baptiste Auguié

    Robert Dober Guest

    On Nov 24, 2007 11:13 PM, baptiste Augui=E9 <> wrote:
    > Hi,
    >
    >
    > A basic question: how to extract several consecutive lines of numbers
    > in a text file (example below) and assign them to some GSL::Matrix in
    > Ruby?
    >
    > text file:
    >
    > > some lines of text ...
    > > ....
    > > more text
    > > below is the data to extract
    > >
    > > 1 2 3
    > > 4 5 6
    > > 7 8 9
    > > 23 7.3e-8 3.4
    > >
    > >
    > > some more text

    >
    >
    > from which i would like to create three vectors, a=3D[1, 4, 7, 23], b=3D
    > [2, 5, 8, 7.33-8], c=3D[3, 6, 9, 3.4]. The line numbers could be used
    > for indexing if that can help.
    >

    p DATA.readlines.map{ |line| line.chomp.split }.transpose
    __END__
    1 2 3
    4 5 6
    7 8 9
    23 7.3e-8 3.4
    > Many thanks,
    >
    > baptiste
    >
    >

    HTH
    Robert



    --=20
    what do I think about Ruby?
    http://ruby-smalltalk.blogspot.com/
    Robert Dober, Nov 25, 2007
    #2
    1. Advertising

  3. Hi,

    Thanks for the hints, however it doesn't quite work for me yet. I tried,
    > p DATA.readlines.map{ |line| line.chomp.split }.transpose
    > __END__
    > 1 2 3
    > 4 5 6
    > 7 8 9
    > 23 7.3e-8 3.4


    which produces a nice matrix as suggested, but I can't figure how to =20
    read that data from an external file rather than below this "__END__" =20=

    thing ; nor can I see how to deal with the extra text lines above and =20=

    below the data. This is actually the main problem, as I would =20
    otherwise go for

    > require("rbgsl")
    > a, b, c =3D GSL::Vector.filescan(filename)



    Thanks again,

    baptiste

    On 25 Nov 2007, at 00:01, Robert Dober wrote:

    > On Nov 24, 2007 11:13 PM, baptiste Augui=E9 <> =

    wrote:
    >> Hi,
    >>
    >>
    >> A basic question: how to extract several consecutive lines of numbers
    >> in a text file (example below) and assign them to some GSL::Matrix in
    >> Ruby?
    >>
    >> text file:
    >>
    >>> some lines of text ...
    >>> ....
    >>> more text
    >>> below is the data to extract
    >>>
    >>> 1 2 3
    >>> 4 5 6
    >>> 7 8 9
    >>> 23 7.3e-8 3.4
    >>>
    >>>
    >>> some more text

    >>
    >>
    >> from which i would like to create three vectors, a=3D[1, 4, 7, 23], =

    b=3D
    >> [2, 5, 8, 7.33-8], c=3D[3, 6, 9, 3.4]. The line numbers could be used
    >> for indexing if that can help.
    >>

    > p DATA.readlines.map{ |line| line.chomp.split }.transpose
    > __END__
    > 1 2 3
    > 4 5 6
    > 7 8 9
    > 23 7.3e-8 3.4
    >> Many thanks,
    >>
    >> baptiste
    >>
    >>

    > HTH
    > Robert
    >
    >
    >
    > --=20
    > what do I think about Ruby?
    > http://ruby-smalltalk.blogspot.com/
    >


    _____________________________

    Baptiste Augui=E9

    Physics Department
    University of Exeter
    Stocker Road,
    Exeter, Devon,
    EX4 4QL, UK

    Phone: +44 1392 264187

    http://newton.ex.ac.uk/research/emag
    http://projects.ex.ac.uk/atto
    ______________________________
    baptiste Auguié, Nov 25, 2007
    #3
  4. baptiste Auguié

    MonkeeSage Guest

    def input_matrix(file)
    File.open(file) { |f|
    f.readlines.collect { |line|
    line = line.chomp
    if line.empty?
    then nil
    else line.split
    end
    }.compact.transpose
    }
    end

    p input_matrix(ARGV[0])

    ^ Composes matrix from first file given on command line, ignoring
    blank lines in file.

    Regards,
    Jordan
    MonkeeSage, Nov 25, 2007
    #4
  5. Thanks, this didn't work either (the data file contains lines of text =20=

    before and after the data).
    I've eventually figured a way playing with different bits of code I =20
    found:


    > # extract space separated numbers from lines 5 to 9 in 'data.txt',
    > # ignoring header and footer text lines


    > current =3D []
    >
    > File.foreach('data.txt') do |line|
    > if ($. =3D=3D 5) .. ($. =3D=3D 9)
    > current << line.scan(/\S+/).map! {|x| x.to_f}
    > end
    > end
    > p current



    Best wishes,

    baptiste

    On 25 Nov 2007, at 12:15, MonkeeSage wrote:

    > def input_matrix(file)
    > File.open(file) { |f|
    > f.readlines.collect { |line|
    > line =3D line.chomp
    > if line.empty?
    > then nil
    > else line.split
    > end
    > }.compact.transpose
    > }
    > end
    >
    > p input_matrix(ARGV[0])
    >
    > ^ Composes matrix from first file given on command line, ignoring
    > blank lines in file.
    >
    > Regards,
    > Jordan
    >


    _____________________________

    Baptiste Augui=E9

    Physics Department
    University of Exeter
    Stocker Road,
    Exeter, Devon,
    EX4 4QL, UK

    Phone: +44 1392 264187

    http://newton.ex.ac.uk/research/emag
    http://projects.ex.ac.uk/atto
    ______________________________
    baptiste Auguié, Nov 25, 2007
    #5
  6. baptiste Auguié

    Robert Dober Guest

    On Nov 25, 2007 2:24 PM, baptiste Augui=E9 <> wrote:
    > Thanks, this didn't work either (the data file contains lines of text
    > before and after the data).


    grep is your friend

    508/8 > cat data.txt && cat detect.rb && ruby detect.rb data.txt
    1 2 3
    Some text
    4 5 6
    more text
    7 8 9
    42 42 42
    23 7.3e-8 3.4
    #!/usr/local/bin/ruby
    # vim: sw=3D2 ts=3D2 ft=3Druby expandtab tw=3D0 nu syn:

    p ARGF.readlines.grep(/^\s*[\.\d]/).map{ |line| line.chomp.split }.transpos=
    e

    [["1", "4", "7", ".42", "23"], ["2", "5", "8", "42", "7.3e-8"], ["3",
    "6", "9", "42", "3.4"]]

    BTW don't forget to tell us the mark you got ;)

    R.
    --=20
    what do I think about Ruby?
    http://ruby-smalltalk.blogspot.com/
    Robert Dober, Nov 25, 2007
    #6
  7. baptiste Auguié

    MonkeeSage Guest

    On Nov 25, 8:22 am, Robert Dober <> wrote:

    > # vim: sw=2 ts=2 ft=ruby expandtab tw=0 nu syn:


    Happiness! Another vim user. :)

    Regards,
    Jordan
    MonkeeSage, Nov 25, 2007
    #7
  8. Thanks, this is exactly what i was looking for: I just never find my =20
    way in these regular expressions, not to mention how to adapt it in =20
    Ruby code!

    On 25 Nov 2007, at 14:22, Robert Dober wrote:

    > On Nov 25, 2007 2:24 PM, baptiste Augui=E9 <> wrote:
    >> Thanks, this didn't work either (the data file contains lines of text
    >> before and after the data).

    >
    > grep is your friend
    >


    > 508/8 > cat data.txt && cat detect.rb && ruby detect.rb data.txt
    > 1 2 3
    > Some text
    > 4 5 6
    > more text
    > 7 8 9
    > .42 42 42
    > 23 7.3e-8 3.4
    > #!/usr/local/bin/ruby
    > # vim: sw=3D2 ts=3D2 ft=3Druby expandtab tw=3D0 nu syn:
    >
    > p ARGF.readlines.grep(/^\s*[\.\d]/).map{ |line| =20
    > line.chomp.split }.transpose
    >
    > [["1", "4", "7", ".42", "23"], ["2", "5", "8", "42", "7.3e-8"], ["3",
    > "6", "9", "42", "3.4"]]
    >
    > BTW don't forget to tell us the mark you got ;)
    >


    do you get a mark for a PhD? I can only tell you the imaginary part =20
    is gonna be negative, in terms of propagation constant.


    _____________________________

    Baptiste Augui=E9

    Physics Department
    University of Exeter
    Stocker Road,
    Exeter, Devon,
    EX4 4QL, UK

    Phone: +44 1392 264187

    http://newton.ex.ac.uk/research/emag
    http://projects.ex.ac.uk/atto
    ______________________________
    baptiste Auguié, Nov 25, 2007
    #8
  9. baptiste Auguié

    Robert Dober Guest

    On Nov 25, 2007 3:53 PM, baptiste Augui=E9 <> wrote:
    > Thanks, this is exactly what i was looking for: I just never find my
    > way in these regular expressions, not to mention how to adapt it in
    > Ruby code!
    >
    > On 25 Nov 2007, at 14:22, Robert Dober wrote:
    >
    > > On Nov 25, 2007 2:24 PM, baptiste Augui=E9 <> wrote:
    > >> Thanks, this didn't work either (the data file contains lines of text
    > >> before and after the data).

    > >
    > > grep is your friend
    > >

    >
    > > 508/8 > cat data.txt && cat detect.rb && ruby detect.rb data.txt
    > > 1 2 3
    > > Some text
    > > 4 5 6
    > > more text
    > > 7 8 9
    > > .42 42 42
    > > 23 7.3e-8 3.4
    > > #!/usr/local/bin/ruby
    > > # vim: sw=3D2 ts=3D2 ft=3Druby expandtab tw=3D0 nu syn:
    > >
    > > p ARGF.readlines.grep(/^\s*[\.\d]/).map{ |line|
    > > line.chomp.split }.transpose
    > >
    > > [["1", "4", "7", ".42", "23"], ["2", "5", "8", "42", "7.3e-8"], ["3",
    > > "6", "9", "42", "3.4"]]
    > >
    > > BTW don't forget to tell us the mark you got ;)
    > >

    >
    > do you get a mark for a PhD? I can only tell you the imaginary part
    > is gonna be negative, in terms of propagation constant.


    Well in that case you just state the sources ;) just kidding, I am
    aware that Ruby is only doing some "low" stuff, but still glad you use
    it as a tool, and concerning your PhD
    "bonne chance".

    R.
    >
    >
    >
    > _____________________________
    >
    > Baptiste Augui=E9
    >
    > Physics Department
    > University of Exeter
    > Stocker Road,
    > Exeter, Devon,
    > EX4 4QL, UK
    >
    > Phone: +44 1392 264187
    >
    > http://newton.ex.ac.uk/research/emag
    > http://projects.ex.ac.uk/atto
    > ______________________________
    >
    >
    >
    >
    >
    >
    >




    --=20
    what do I think about Ruby?
    http://ruby-smalltalk.blogspot.com/
    Robert Dober, Nov 25, 2007
    #9
    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. baptiste Auguié

    extracting values from consecutive lines

    baptiste Auguié, Oct 17, 2007, in forum: Ruby
    Replies:
    15
    Views:
    237
    7stud --
    Oct 18, 2007
  2. Peter

    Finding consecutive lines

    Peter, Nov 5, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    221
    Michele Dondi
    Nov 6, 2004
  3. sbk
    Replies:
    5
    Views:
    153
  4. shree
    Replies:
    5
    Views:
    145
    shree
    Dec 29, 2007
  5. Replies:
    9
    Views:
    146
    A. Sinan Unur
    Aug 22, 2008
Loading...

Share This Page