one liner? --- split file at empty line 
Author Message
 one liner? --- split file at empty line

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 06:50:39 GMT  
 one liner? --- split file at empty line
Hi,

At Fri, 18 Oct 2002 08:08:01 +0900,

Quote:

> I need to split a file

>    Part A
>    ....
>    ....

>    Part B
>    ....
>    ....

> to files A, B, ...

ruby -slne 'open($1,"w"){|f|f.puts $_}if/\APart\s+(\S+)/' -- -/=

# No standard command line option to make paragraph mode...

--
Nobu Nakada



Tue, 05 Apr 2005 07:55:02 GMT  
 one liner? --- split file at empty line
Hi,

At Fri, 18 Oct 2002 08:55:02 +0900,

Quote:

> # No standard command line option to make paragraph mode...

Sorry, I forgot -00 option.

$ ruby -00lne 'open($1,"w"){|f|f.puts $_}if/\APart\s+(\S+)/'

--
Nobu Nakada



Fri, 08 Apr 2005 06:12:21 GMT  
 one liner? --- split file at empty line
Quote:

> $ ruby -00lne 'open($1,"w"){|f|f.puts $_}if/\APart\s+(\S+)/'

Thanks.  It looks good to me.  But it is missing the first Part.
The file looks like:

Some header
Part Cu
 ...

Part In
...

Part O
...

Part Se
...

So far I got file In, O, and Se, but not Cu.

Dong



Fri, 08 Apr 2005 10:11:07 GMT  
 one liner? --- split file at empty line
Hi,

At Mon, 21 Oct 2002 11:32:48 +0900,

Quote:

> Thanks.  It looks good to me.  But it is missing the first Part.
> The file looks like:

> Some header

I assumed each part start with "Part" line.  If preceeding
lines exist, replace '\A' with '^'.

--
Nobu Nakada



Fri, 08 Apr 2005 10:56:37 GMT  
 one liner? --- split file at empty line
Quote:

> I assumed each part start with "Part" line.  If preceeding
> lines exist, replace '\A' with '^'.

Thanks.   Apparently I did not understand the meaning of string -- I thought
it is part of a line.

Now it works great.   I do have another question:  My data files are named
like 30.txt, 40.txt.  How can I generate files with name Cu30.txt, In30.txt?
Where is the arguement stored?  I guess I only need to substitute $1 with
$1 + filename)

ruby -00lne 'open($1,"w"){|f|f.puts $_}if/\APart\s+(\S+)/' 30.txt

Dong



Fri, 08 Apr 2005 12:10:54 GMT  
 one liner? --- split file at empty line
Hi,

At Mon, 21 Oct 2002 13:32:58 +0900,

Quote:

> Now it works great.   I do have another question:  My data files are named
> like 30.txt, 40.txt.  How can I generate files with name Cu30.txt, In30.txt?
> Where is the arguement stored?  I guess I only need to substitute $1 with
> $1 + filename)

ARGF.filename

--
Nobu Nakada



Fri, 08 Apr 2005 12:51:54 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. aplit line - one liner

2. one liner? --- need help splitingi a file

3. splitting strings with more than one splitting char

4. Empty line after writing to file

5. How to replace one or two words with one word with one line of awk code

6. Need to split file with two reports per line

7. Parsing files and lines using split

8. Splitting lines in a file

9. Question: lines of numbers separated with empty lines

10. do NOT download, this file is EMPTY - empty.txt (1/1)

11. empty file returns nil not empty string?

12. how to split each new line into a list, considering quoted new line characters

 

 
Powered by phpBB® Forum Software