SED multiple file redirection? 
Author Message
 SED multiple file redirection?

For sed gurus ...........

I know that it is possible to redirect output
to multiple files in AWK.

e.g.

the following will separate a BIGFILE
into name.1, name.2 .. name.N,
where BIGFILE is separated by lines containing 'NEWFILE'.

awk 'BEGIN {filenum="1"
            filename=sprintf("name.%d",filenum)}
    { print > filename
      if (substr($0,1,7) == "NEWFILE") {
         filename=sprintf("name.%d",++filenum) }}' inputfile

My question is how can you redirect to multiple files
in sed in a similar manner?

Will it be quicker in sed?

Thanks.

--
-------------------------------------------------------------
"Don't be suckered in by the comments - debug only the code."
To reply by e-mail remove '000' from my address
-------------------------------------------------------------



Tue, 12 Feb 2002 03:00:00 GMT  
 SED multiple file redirection?


   >For sed gurus ...........
   >I know that it is possible to redirect output
   >to multiple files in AWK.
   >e.g.
   >the following will separate a BIGFILE
   >into name.1, name.2 .. name.N,
   >where BIGFILE is separated by lines containing 'NEWFILE'.
   >awk 'BEGIN {filenum="1"
   >filename=sprintf("name.%d",filenum)}
   >{ print > filename
   >if (substr($0,1,7) == "NEWFILE") {
   >filename=sprintf("name.%d",++filenum) }}' inputfile
   >My question is how can you redirect to multiple files
   >in sed in a similar manner?
   >Will it be quicker in sed?
man sed
In sed, you use the "w" command to write to a file other than standard
output. The tricky part is creating filenames like name.001, name.002,
etc. without arithmetic or variables.  It can be done (I think), but it's
much easier in awk.  In fact, to do this in sed, you might have to write a
script that creates another sed script.

Net-Tamer V 1.08X - Test Drive



Tue, 12 Feb 2002 03:00:00 GMT  
 SED multiple file redirection?

Quote:

> For sed gurus ...........

> I know that it is possible to redirect output
> to multiple files in AWK.

> e.g.

> the following will separate a BIGFILE
> into name.1, name.2 .. name.N,
> where BIGFILE is separated by lines containing 'NEWFILE'.

> awk 'BEGIN {filenum="1"
>             filename=sprintf("name.%d",filenum)}
>     { print > filename
>       if (substr($0,1,7) == "NEWFILE") {
>          filename=sprintf("name.%d",++filenum) }}' inputfile

> My question is how can you redirect to multiple files
> in sed in a similar manner?

> Will it be quicker in sed?

If "quicker" is important, have you looked at "csplit", which does
pretty much what (I understand) you want?

[1041]C:/+: csplit

GNU text utilities 1.19 - csplit

Usage: csplit [OPTION]... FILE PATTERN...
Output pieces of FILE separated by PATTERN(s) to files `xx01', `xx02', ...,
and output byte counts of each piece to standard output.
 ...lots more on options...

--
Arthur Pool
Brisbane/Queensland/Australia



Wed, 13 Feb 2002 03:00:00 GMT  
 SED multiple file redirection?

Quote:

> awk 'BEGIN {filenum="1"
>             filename=sprintf("name.%d",filenum)}
>     { print > filename
>       if (substr($0,1,7) == "NEWFILE") {
>          filename=sprintf("name.%d",++filenum) }}' inputfile

You don't need to initialize filenum (it's guaranteed to be
autoinitialized to 0), and if you did, you wouldn't want to initialize
it to the string "1", but rather to the number 1.

Since you're not doing any special formatting of the variable
portion (the integer suffix) of filename, you don't need to use
sprintf. Simply concatenate the just-incremented value of filenum
with the string literal "name.".

There is a limit to the number of file handles you can have open
at one time, so it's important to close them when you're through
with them. Use close().

Here's an untested revision of your awk script:

BEGIN      { filename = "name." ++filenum }
           { print >filename }
/^NEWFILE/ { close(filename); filename = "name." ++filenum }

Quote:
> My question is how can you redirect to multiple files
> in sed in a similar manner?

> Will it be quicker in sed?

I invariably use awk or Perl for splitting large files based on
strings within the file that match some regular expression pattern,
but I do so because I use the matched strings to somehow determine
the output file name. In your case, because there is no relationship
between the marker string "NEWFILE" and the output file names
(name.1, name.2, ..., name.n), and because the output file names
are just integers with string prefixes, you may as well use csplit(1).

(The first thing I try to teach new programmers is how to not
program. It's the hardest lesson to learn, because to them it seems
contrary to their purpose. It isn't.)

--
Jim Monty

Tempe, Arizona USA



Thu, 14 Feb 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Sed/AWK - replace positions in multiple lines.

2. sed multiple expressions

3. Sed: Inserting file in another file

4. sed: input file = output file

5. Multiple edits, multiple files

6. Multiple textures on one IndexFaceSet [multiple files, not a map]

7. TCLLib cmdline - multiple file names and multiple invocations

8. help on sed file

9. log file processing - awk or sed

10. Find the first occurence of file in pwd with sed

11. Using awk And/Or sed To Edit Fourth Field Of File

12. sed - extracting "*.cpp" from a file

 

 
Powered by phpBB® Forum Software