one liner? --- need help splitingi a file 
Author Message
 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



Tue, 05 Apr 2005 07:07:53 GMT  
 one liner? --- need help splitingi a file
Hello,

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


  Department of Materials Technology,
  Faculty of Engineering,  Chiba-University
------------------------------------------------------



Tue, 05 Apr 2005 11:20:28 GMT  
 one liner? --- need help splitingi a file

Quote:

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

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



Tue, 05 Apr 2005 21:08:24 GMT  
 one liner? --- need help splitingi a file

Quote:

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

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

:-)



Tue, 05 Apr 2005 22:33:49 GMT  
 one liner? --- need help splitingi a file

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



Tue, 05 Apr 2005 22:59:11 GMT  
 one liner? --- need help splitingi a file

Quote:

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

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. ;-) (See Stefan's link.)

Massimiliano



Wed, 06 Apr 2005 00:00:23 GMT  
 one liner? --- need help splitingi a file

Quote:

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



Wed, 06 Apr 2005 00:02:56 GMT  
 one liner? --- need help splitingi a file
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



Fri, 08 Apr 2005 05:31:53 GMT  
 one liner? --- need help splitingi a file

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

Quote:
> Thanks again.

> Dong

Gavin


Fri, 08 Apr 2005 11:14:07 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. APL one-liner needed for new novel

2. one liner? --- split file at empty line

3. HELP Newbie solve this Problem - One-Liners

4. I need help with reading multiple records off one data file

5. J One Liners

6. one liner awk

7. One.liner

8. aplit line - one liner

9. Looking for AWK/SED one-liners

10. One Liners

11. LOGO-L> One-liners :-)

12. LOGO-L> one-liners

 

 
Powered by phpBB® Forum Software