# Extract consecutive lines of data

**URL:** <https://www.ruby-forum.com/t/extract-consecutive-lines-of-data/121179>\
**Category:** Ruby\
**Created:** [November 24, 2007, 11:14pm UTC](https://www.ruby-forum.com/t/extract-consecutive-lines-of-data/121179 "2007-11-24T23:14:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [November 24, 2007, 11:14pm UTC](https://www.ruby-forum.com/t/extract-consecutive-lines-of-data/121179/1 "2007-11-24T23:14:54Z")

</div>

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 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

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [November 25, 2007, 1:03am UTC](https://www.ruby-forum.com/t/extract-consecutive-lines-of-data/121179/2 "2007-11-25T01:03:06Z")

</div>

On Nov 24, 2007 11:13 PM, baptiste Auguié [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > …
> 
> 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.

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

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [November 25, 2007, 12:31pm UTC](https://www.ruby-forum.com/t/extract-consecutive-lines-of-data/121179/3 "2007-11-25T12:31:36Z")

</div>

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  
read that data from an external file rather than below this “ **END** ”  
thing ; nor can I see how to deal with the extra text lines above and  
below the data. This is actually the main problem, as I would  
otherwise go for

> require(“rbgsl”)  
> a, b, c = GSL::Vector.filescan(filename)

Thanks again,

baptiste

On 25 Nov 2007, at 00:01, Robert D. wrote:

> > > some lines of text …  
> > > some more text  
> > > 7 8 9
> 
> –  
> what do I think about Ruby?  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

Baptiste Auguié

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

Phone: +44 1392 264187

[http://newton.ex.ac.uk/research/emag](http://newton.ex.ac.uk/research/emag)  
[http://projects.ex.ac.uk/atto](http://projects.ex.ac.uk/atto)

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [November 25, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/extract-consecutive-lines-of-data/121179/4 "2007-11-25T13:15:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [November 25, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/extract-consecutive-lines-of-data/121179/5 "2007-11-25T14:25:06Z")

</div>

Thanks, this didn’t work either (the data file contains lines of text  
before and after the data).  
I’ve eventually figured a way playing with different bits of code I  
found:

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

> current = []
> 
> File.foreach(‘data.txt’) do |line|  
> if ($. == 5) … ($. == 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:

> end
> 
> p input\_matrix(ARGV[0])
> 
> ^ Composes matrix from first file given on command line, ignoring  
> blank lines in file.
> 
> Regards,  
> Jordan

* * *

Baptiste Auguié

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

Phone: +44 1392 264187

[http://newton.ex.ac.uk/research/emag](http://newton.ex.ac.uk/research/emag)  
[http://projects.ex.ac.uk/atto](http://projects.ex.ac.uk/atto)

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [November 25, 2007, 3:23pm UTC](https://www.ruby-forum.com/t/extract-consecutive-lines-of-data/121179/6 "2007-11-25T15:23:07Z")

</div>

On Nov 25, 2007 2:24 PM, baptiste Auguié [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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=2 ts=2 ft=ruby expandtab tw=0 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 😉

R.

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [November 25, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/extract-consecutive-lines-of-data/121179/7 "2007-11-25T15:45:28Z")

</div>

On Nov 25, 8:22 am, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

Happiness! Another vim user. 🙂

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [November 25, 2007, 3:54pm UTC](https://www.ruby-forum.com/t/extract-consecutive-lines-of-data/121179/8 "2007-11-25T15:54:52Z")

</div>

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 D. wrote:

> On Nov 25, 2007 2:24 PM, baptiste Auguié [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Thanks, this didn’t work either (the data file contains lines of text  
> > before and after the data).
> 
> grep is your friend

> 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.

* * *

Baptiste Auguié

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

Phone: +44 1392 264187

[http://newton.ex.ac.uk/research/emag](http://newton.ex.ac.uk/research/emag)  
[http://projects.ex.ac.uk/atto](http://projects.ex.ac.uk/atto)

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [November 25, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/extract-consecutive-lines-of-data/121179/9 "2007-11-25T16:47:45Z")

</div>

On Nov 25, 2007 3:53 PM, baptiste Auguié [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > grep is your friend  
> > #!/usr/local/bin/ruby
> 
> 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.
