One liner? --- need help splitingi a file

I need to split a file

Part A
....
....

Part B
....
....

to files A, B, …

Would somebody show me how to do it in Ruby? I have used Perl before
for data parsing, but have forgotten most of the usage. I am interested
in Ruby, wishing it’s easier to learn and maintain than Perl.

Thanks.

Dong

Hello,

I need to split a file

Part A

Part B

to files A, B, …

Would somebody show me how to do it in Ruby? I have used Perl before
for data parsing, but have forgotten most of the usage. I am interested
in Ruby, wishing it’s easier to learn and maintain than Perl.

Thanks.

Dong

How about this,

fname = nil
File.open(“orig”) {|f|
while(l = f.gets)
if l =~ /^Part ([A-Z])/
file.close if fname
fname = $1
file = File.open(fname,“w”)
end
file.puts l if fname
end
}
file.close if fname

···

Don’t use this if efficiency is critical:

$ cat file | ruby -e ‘STDIN.read.split(/\n\n/).each_with_index {|part,i| File.open(“f.#{i}”, “w”).puts(part) }’

Massimiliano

···

On Fri, Oct 18, 2002 at 08:27:58AM +0900, Dong wrote:

Would somebody show me how to do it in Ruby? I have used Perl before
for data parsing, but have forgotten most of the usage. I am interested
in Ruby, wishing it’s easier to learn and maintain than Perl.

Thanks for all who replied. Ruby is quite impressive. I am reading
the online Programming Ruby and the turorial at Rubycentral. My use
of Ruby for now would be mostly data parsing, nothing advanced.
Anybody have pointers to other sources?

Thanks again.

Dong

http://www.ling.helsinki.fi/~reriksso/unix/award.html

:slight_smile:

···

Massimiliano Mirra list@nospamchromatic-harp.com wrote:

$ cat file | ruby -e ‘STDIN.read.split(/\n\n/).each_with_index {|part,i| File.open(“f.#{i}”, “w”).puts(part) }’

Don’t use this if efficiency is critical:

$ cat file | ruby -e ‘STDIN.read.split(/\n\n/).each_with_index
{|part,i| File.open(“f.#{i}”, “w”).puts(part) }’

You can save a few CPU cycles by removing cat there, no? Or am I
missing something subtle.

···

=====

Use your computer to help find a cure for cancer: http://members.ud.com/projects/cancer/

Yahoo IM: michael_s_campbell


Do you Yahoo!?
Faith Hill - Exclusive Performances, Videos & More
http://faith.yahoo.com

Thanks for all who replied. Ruby is quite impressive. I am reading
the online Programming Ruby and the turorial at Rubycentral. My use
of Ruby for now would be mostly data parsing, nothing advanced.
Anybody have pointers to other sources?

“The Ruby Way” (dead tree book) has a lot of “how to do this” kind of
information. Very helpful.

Thanks again.

Dong

Gavin

···

From: “Dong” dongxiang@fastmail.fm

By reading the file directly? Yes, you’re not missing anything. I
just like not to care whence the information comes from, be it a file
or a socket or a command’s output or whatever; the proposed form won’t
care about it either, and will just shut up and work.

Plus I love awards. :wink: (See Stefan’s link.)

Massimiliano

···

On Fri, Oct 18, 2002 at 11:59:11PM +0900, Michael Campbell wrote:

$ cat file | ruby -e ‘STDIN.read.split(/\n\n/).each_with_index
{|part,i| File.open(“f.#{i}”, “w”).puts(part) }’
You can save a few CPU cycles by removing cat there, no? Or am I
missing something subtle.

Or you just meant ruby -e … <file? Yes again (I don’t know if that
works with every shell, though).

Massimiliano

···

On Fri, Oct 18, 2002 at 11:59:11PM +0900, Michael Campbell wrote:

$ cat file | ruby -e ‘STDIN.read.split(/\n\n/).each_with_index
{|part,i| File.open(“f.#{i}”, “w”).puts(part) }’
You can save a few CPU cycles by removing cat there, no? Or am I
missing something subtle.