Redirecting to TWO output files 
Author Message
 Redirecting to TWO output files

Hi,

I have a program how write some lines to the terminal (stdout).

Not all the lines are of this kind. I would like to use
a script with awk and/or sed to be able to redirect EACH group

to a different output file. Now, what I do is running the program

file.

Can anybody help me? Thanks.

  J C
--
357-"Funny, doesn't *look* like a {*filter*}psycho...." {but it
was one}
        ("FAMOUS LAST WORDS", collected by O.Rosenkranz)
====================================================================
   *    J. C. Gonzalez                  
  .     ------------------------------------------------------------  
  .     Max-Planck-Institut fuer Physik        Tel.: +49 89 32354445
   .    (Werner-Heisenberg-Institut)           Fax : +49 89 3226704  
     *  Foehringer Ring, 6          ... . .... . ..... ......... ..

   .    Deutschland                    WWW: www.gae.ucm.es/~gonzalez
====================================================================



Mon, 30 Apr 2001 03:00:00 GMT  
 Redirecting to TWO output files
{

  print >"at1_file"
 else {

   print >"at2_file"
  else
   print
 }

Quote:
}

The above programs doesn't do much more than separate the file into 3


at2_file; all other lines are printed to stdout. If you need other

lines before redirecting them to the files, you can easily add that). You
should be aware that if you run this program a second time, the files
at1_file and at2_file will be overwritten.

Cesar

--
Please remove the UPPERCASE characters from my e-mail address for the real
thing

Quote:

>Hi,

>I have a program how write some lines to the terminal (stdout).

>Not all the lines are of this kind. I would like to use
>a script with awk and/or sed to be able to redirect EACH group

>to a different output file. Now, what I do is running the program

>file.

>Can anybody help me? Thanks.

>  J C
>--
>357-"Funny, doesn't *look* like a {*filter*}psycho...." {but it
>was one}
> ("FAMOUS LAST WORDS", collected by O.Rosenkranz)
>====================================================================
>   *    J. C. Gonzalez
>  .     ------------------------------------------------------------
>  .     Max-Planck-Institut fuer Physik        Tel.: +49 89 32354445
>   .    (Werner-Heisenberg-Institut)           Fax : +49 89 3226704
>     *  Foehringer Ring, 6          ... . .... . ..... ......... ..

>   .    Deutschland                    WWW: www.gae.ucm.es/~gonzalez
>====================================================================



Mon, 30 Apr 2001 03:00:00 GMT  
 Redirecting to TWO output files

Quote:
> I have a program how write some lines to the terminal (stdout).

> Not all the lines are of this kind. I would like to use
> a script with awk and/or sed to be able to redirect EACH group

> to a different output file. Now, what I do is running the program

> file.

The best awk solution to this problem depends on the value of n
and whether or not this value exceeds the maximum number of files
your version of awk and your operating system allows you to have
open simultaneously. If the value of n does not exceed this limit,
then the solution is simple and straightforward.


and so on." Let's assume that, in these cases, the value of $1 is

following script will print each line of input to files named

    { print >$1 ".txt" }

Easy, huh?

Now let's assume that not all of the lines begin with these "tags",
but that you want to change which file the output is redirected to
each time a tag line occurs in the input. This script demonstrates
how you might do this:


    { print >file }

Obviously, the regular expression pattern you use here depends
on what actually constitutes a tag line.

You may have other names you want to use for the output files.
This is where awk's associative arrays are invaluable. Here's a
demonstration:

    BEGIN {



        ...

    }

    $1 in FILE { file = FILE[$1] }

    { print >file }

Unlike the previous solutions, this one requires that you know in
advance the full range of tags that exist in your input.

If the number of output files (i.e., discrete tags) exceeds the limit
of files that can be open at one time, then the awk solution to this
problem becomes more complicated.

See also csplit(1).

--
Jim Monty

http://www.primenet.com/~monty/
Tempe, Arizona USA



Tue, 01 May 2001 03:00:00 GMT  
 Redirecting to TWO output files
< (... snip, snip ...)

Quote:

>     BEGIN {



>         ...

>     }

>     $1 in FILE { file = FILE[$1] }

>     { print >file }

Thanks!! This is really great.

Quote:
> Unlike the previous solutions, this one requires that you know in
> advance the full range of tags that exist in your input.

That's not a big problem for me.

But, let's put it now in a more theoretical way.
Is there any possibility of using the output of a command,
in a command line, by two different pipes?
Something like putting together in one single line
$ cat file | grep expr1 > fileout1
and
$ cat file | grep expr2 > fileout2

(and eventually more than two times...)

As I say, at this point I'm just curious.

Cheers,

  J C
--

        ("FAMOUS LAST WORDS", collected by O.Rosenkranz)
====================================================================
   *    J. C. Gonzalez                  
  .     ------------------------------------------------------------  
  .     Max-Planck-Institut fuer Physik        Tel.: +49 89 32354445
   .    (Werner-Heisenberg-Institut)           Fax : +49 89 3226704  
     *  Foehringer Ring, 6          ... . .... . ..... ......... ..

   .    Deutschland                    WWW: www.gae.ucm.es/~gonzalez
====================================================================



Tue, 01 May 2001 03:00:00 GMT  
 Redirecting to TWO output files

Quote:

>     BEGIN {



>         ...

>     }

Counting in German:0-10
null  eins  zwei  drei  vier
fuenf  (ue="u=u-Umlaut)
sechs  sieben  acht neun zehn

"nein" means "no"

nice try though

lms



Wed, 02 May 2001 03:00:00 GMT  
 Redirecting to TWO output files

Quote:

> I have a program how write some lines to the terminal (stdout).

> Not all the lines are of this kind. I would like to use
> a script with awk and/or sed to be able to redirect EACH group

> to a different output file.





w aux.out'  infile


                               fileName=number ".out"
                              print >fileName
                              next
                            }
                           { print >"aux.out"
                           }'  infile

not tested
 LMS
free sed/awk book:
      ftp://ftp.u-aizu.ac.jp/u-aizu/doc/Tech-Report/1997/97-2-007.ps.gz
      ftp://ftp.u-aizu.ac.jp/u-aizu/doc/Tech-Report/1997/97-2-007.tar.gz



Wed, 02 May 2001 03:00:00 GMT  
 Redirecting to TWO output files

Quote:

> >     BEGIN {



> >         ...

> >     }

> Counting in German:0-10
> null  eins  zwei  drei  vier
> fuenf  (ue="u=u-Umlaut)
> sechs  sieben  acht neun zehn

> "nein" means "no"

> nice try though

Then my German was correct. I wanted to name the files "one.txt",
"two.txt", "three.txt", ..., "no.txt".  ;-)

--
Jim Monty

http://www.primenet.com/~monty/
Tempe, Arizona USA



Wed, 02 May 2001 03:00:00 GMT  
 Redirecting to TWO output files

Quote:

> > I have a program how write some lines to the terminal (stdout).

> > Not all the lines are of this kind. I would like to use
> > a script with awk and/or sed to be able to redirect EACH group

> > to a different output file.





> w aux.out'  infile



you've used here will not distinguish these lines and separate
them into files in the way that Jose obviously intends. What's
worse, you're unnecessarily attempting to match the same lines
multiple times with different patterns, and then once again with
a pattern that will successfully match the same lines you've
already matched once before! For example, a line that begins with

and twice unsuccessfully. Something like this would be an
improvement:

     #n

     w 1.out
     b
     }

     w 2.out
     b
     }

     w 3.out
     b
     }
     w aux.out
     # not tested


be evaluated once, and no line will ever be evaluated more than
three times.

Quote:

>                                fileName=number ".out"
>                               print >fileName
>                               next
>                             }
>                            { print >"aux.out"
>                            }'  infile

It's difficult to read this script because of the strange
formatting and the misalignment of the statements. Are you
preparing your Usenet news articles using a proportional font
and a Japanese character encoding? If so, please realize that
most Usenet news readers are NOT using your same font, font
size, screen resolution, character encoding, etc.

Here is the same script formatted more conventionally:


         number = substr($0, 2, 1)
         fileName = number ".out"
         print >fileName
         next
     }

     {
         print >"aux.out"
     }

This is much easier to read and understand.

The regular expression you use here suffers the same problem
as those in your sed script: it will have the effect of putting

file; namely, the file "1.out". You're proffering a solution
based on assumptions without stating those assumptions, which
can be misleading.

Personally, I would have found your awk solution easier to
understand if it were expressed more succinctly, like this:

     {

             print >substr($0, 2, 1) ".out"
         else
             print >"aux.out"
     }

Or even like this:


Admittedly, programmers who aren't accustomed to the conditional
statement of C, Perl, and awk will perhaps find this version TOO
succint. But expressing the program in this way serves to emphasize
that, indeed, the problem is easily reduced to a single print
statement, where the only variable is the name of the file to which
output from that print statement is redirected. My versions also
eliminate unnecessary assignments to variables ("number" and
"fileName" in your version) and the dubious use of a next statement
where a more stuctured control construct is appropriate.

Nice try though!

--
Jim Monty

http://www.primenet.com/~monty/
Tempe, Arizona USA



Wed, 02 May 2001 03:00:00 GMT  
 Redirecting to TWO output files

Quote:
> < (... snip, snip ...)

> >     BEGIN {



> >         ...

> >     }

> >     $1 in FILE { file = FILE[$1] }

> >     { print >file }

> Thanks!! This is really great.

> > Unlike the previous solutions, this one requires that you know in
> > advance the full range of tags that exist in your input.

> That's not a big problem for me.

> But, let's put it now in a more theoretical way.
> Is there any possibility of using the output of a command,
> in a command line, by two different pipes?
> Something like putting together in one single line
> $ cat file | grep expr1 > fileout1

  grep expr1 file > fileout1

Quote:
> and
> $ cat file | grep expr2 > fileout2

  grep expr2 file > fileout2

  (one pipe less; one command less)
  Famous: Useless use of cat command.

- Show quoted text -

Quote:

> (and eventually more than two times...)

> As I say, at this point I'm just curious.

> Cheers,

>   J C
> --

>    ("FAMOUS LAST WORDS", collected by O.Rosenkranz)
> ====================================================================
>    *    J. C. Gonzalez                  
>   .     ------------------------------------------------------------  
>   .     Max-Planck-Institut fuer Physik        Tel.: +49 89 32354445
>    .    (Werner-Heisenberg-Institut)           Fax : +49 89 3226704  
>      *  Foehringer Ring, 6          ... . .... . ..... ......... ..

>    .    Deutschland                    WWW: www.gae.ucm.es/~gonzalez
> ====================================================================

Paulino.


Wed, 02 May 2001 03:00:00 GMT  
 Redirecting to TWO output files
Jim Monty commented about:

Quote:





> > w aux.out'  infile

This is simple, and I assume that the end-user can judge
whether it is useful to him/her.  If the patterns are overlapping

of expertise on the user's side) the patterns have to be altered
observing that sed/awk have upper limits on files one can write to.

If the file processed is very large and the application is time critical,
then the
use of the b command is advisable of course.  Such judgement is
on the side of the end-user.

LMS
free sed/awk book:
      ftp://ftp.u-aizu.ac.jp/u-aizu/doc/Tech-Report/1997/97-2-007.ps.gz
      ftp://ftp.u-aizu.ac.jp/u-aizu/doc/Tech-Report/1997/97-2-007.tar.gz



Tue, 08 May 2001 03:00:00 GMT  
 Redirecting to TWO output files

Quote:

> >                                fileName=number ".out"
> >                               print >fileName
> >                               next
> >                             }
> >                            { print >"aux.out"
> >                            }'  infile

############################################################
I can see the program nicely arranged on my screen.
############################################################

Quote:
> It's difficult to read this script because of the strange
> formatting and the misalignment of the statements. Are you
> preparing your Usenet news articles using a proportional font
> and a Japanese character encoding? If so, please realize that
> most Usenet news readers are NOT using your same font, font
> size, screen resolution, character encoding, etc.

############################################################
I indeed use Japanese encoding.  However, I find your comments
unecessary.
############################################################

Quote:

> Here is the same script formatted more conventionally:


>          number = substr($0, 2, 1)
>          fileName = number ".out"
>          print >fileName
>          next
>      }

>      {
>          print >"aux.out"
>      }

> This is much easier to read and understand.

############################################################
Thanks: now it looks perfect.  The perfect solution out of  synergy.
############################################################

Quote:

> The regular expression you use here suffers the same problem
> as those in your sed script: it will have the effect of putting

> file; namely, the file "1.out". You're proffering a solution
> based on assumptions without stating those assumptions, which
> can be misleading.

############################################################
I assume that the user is intelligent: after having read the solution and

understood it,  he/she should be able to alter the patterns, or
the program as a whole.
############################################################

Quote:
> Personally, I would have found your awk solution easier to
> understand if it were expressed more succinctly, like this:

>      {

>              print >substr($0, 2, 1) ".out"
>          else
>              print >"aux.out"
>      }

############################################################
awk was made to put cases into patterns left and to avoid to write ifs.
I strongly object against advocating
the "empty pattern" { if-then-else } style.
Your proposal is C-style. not awk-style.
Furthermore, I find it extremly useful to set
variables with meaningful names: such as
outputfile=substr($0, 2, 1) ".out" even if used
only once.

This has two advantages:
a)
The program is understandable for me a year from now.
b)
Some awk's do not handle >-concatenation properly.
############################################################

- Show quoted text -

Quote:

> Or even like this:


> Admittedly, programmers who aren't accustomed to the conditional
> statement of C, Perl, and awk will perhaps find this version TOO
> succint. But expressing the program in this way serves to emphasize
> that, indeed, the problem is easily reduced to a single print
> statement, where the only variable is the name of the file to which
> output from that print statement is redirected. My versions also
> eliminate unnecessary assignments to variables ("number" and
> "fileName" in your version) and the dubious use of a next statement
> where a more stuctured control construct is appropriate.

############################################################
As commented above (and in famous C-books): variables can serve
the purpose of readability/maintainability/correctness.  However your
solution is short and sweet.
############################################################
END
############################################################

############################################################

############################################################

############################################################

############################################################

############################################################

############################################################

############################################################

############################################################

############################################################

############################################################

############################################################

############################################################



Tue, 08 May 2001 03:00:00 GMT  
 Redirecting to TWO output files

Quote:


>> > I have a program how write some lines to the terminal (stdout).

>> > Not all the lines are of this kind. I would like to use
>> > a script with awk and/or sed to be able to redirect EACH group

>> > to a different output file.



"aux.out") }


limit of awk?

How about:

nawk '
{

  {outfile = "f" substr($0,RSTART,RLENGTH) ".txt"
   print >> outfile; close(outfile)
  }

Perhaps not very efficient, but more bulletproof.

Tom



Wed, 16 May 2001 03:00:00 GMT  
 Redirecting to TWO output files

Quote:

> Did you answer the problem you raised, of multidigit numbers after the

> limit of awk?

I addressed this point in the first sentence of my original response

excerpt is from URL <http://x12.dejanews.com/getdoc.xp?AN=411272054&
CONTEXT=912364532.796393545&hitnum=0>:

     The best awk solution to this problem depends on the value of
     n and whether or not this value exceeds the maximum number of
     files your version of awk and your operating system allows
     you to have open simultaneously. If the value of n does not
     exceed this limit, then the solution is simple and
     straightforward.

     [...]

     If the number of output files (i.e., discrete tags) exceeds
     the limit of files that can be open at one time, then the awk
     solution to this problem becomes more complicated.

I should have written "only slightly more complicated."

Quote:
> How about:

> nawk '
> {

>         outfile = "f" substr($0, RSTART, RLENGTH) ".txt"
>         print >>outfile
>         close(outfile)
>     }

> Perhaps not very efficient, but more bulletproof.


etc. and unnecessarily opens (implicitly) and closes (explicitly)
the output file each time a line is printed, regardless of whether
or not the name of the output file actually changes. When he stated
his problem, Jose explained that there were lines other than those
with tags on them that must also be printed.

Here's the demonstrative script that I posted in the same article
quoted above, and the one that Jose later indicated he used as the
basis for his solution:

    BEGIN {



        ...

    }

    $1 in FILE { file = FILE[$1] }

    { print >file }

To alter this script so that only one output file is open at a
time, just change the last two lines:

    $1 in FILE && $1 != tag {
        close(file)
        tag  = $1
        file = FILE[tag]
    }

    { print >>file }

This script prints every line of input to an output file. The output
file to which the lines are printed is toggled when the line begins
with a tag, but only when the current tag is different from the
previous tag. And, of course, only one output file is open at a
time.

One caveat: if the files named "eins.txt", "zwei.txt", "drei.txt",
etc. exist when the script is invoked, this new version will append
to them, not overwrite them.

--
Jim Monty

http://www.primenet.com/~monty/
Tempe, Arizona USA



Fri, 18 May 2001 03:00:00 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Redirecting Output to files

2. Redirecting program output to file

3. how to redirect lpt1 to file-output

4. redirect g77 output into a file under windows xp

5. redirect output of functions to a string or file

6. Redirect file output into variables

7. redirect g77 under DOS 6.22- need output file

8. expect: redirect output into file called `uname -n'.txt

9. redirect exec output to a file

10. Redirecting output to a scratch file

11. Redirecting output to file with elisp

12. Redirecting TCL script output to a file

 

 
Powered by phpBB® Forum Software