cat, pipes, and filters

Root of all Evil root at progress.COM
Sat Jun 1 02:54:46 AEST 1991


Hi,

   I've got a question regarding the way cat behaves in a pipeline.
(I know, his fur gets all oily %+})  Can I cat the contents of a
file | pipe the output to a filter (such as sed) | then cat the
filtered output back to the original file?  I've tried this with the
following commands:

   cat $FILE | sed s/"$ENTRY"/"$NEWENTRY"/ > $FILE

   cat $FILE | sed s/"$ENTRY"/"$NEWENTRY"/ | cat > $FILE

Both command produce identical results: $FILE is truncated to 0-length.

   However, the following command gives me the result I want:

   cat $FILE | sed s/"$ENTRY"/"$NEWENTRY"/ | tee $FILE 1>/dev/null

So now my script works but I don't really understand why.  I've tested
this on SunOs 4.1 and Interactive SysV 2.2, no difference.  Is there
something simple about pipes or I/O redirection that I'm not grasping?
Or is this a feature of cat?  

   Any enlightenment would be appreciated.  Also, if you can think of
a better way to do the same thing (short of using perl), please let
me know.

                              Curious Rich

------------------------------------------------------------------------------
Rich Lenihan                 UUCP: mit-eddie!progress!rich
Progress Software Corp.      Internet: rich at progress.com
5 Oak Park                   >-   Insert funny stuff here  -<
Bedford, MA  01730           >-Draw amusing symbols, logo's-<
USA                          >-     or characters here     -<



More information about the Comp.unix.shell mailing list