Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uwm.edu!spool.mu.edu!agate!linus!progress!root From: root@progress.COM (Root of all Evil) Newsgroups: comp.unix.shell Subject: cat, pipes, and filters Summary: redirecting file to itself Keywords: cat pipe filter Message-ID: <1991May31.165446.1530@progress.com> Date: 31 May 91 16:54:46 GMT Sender: usenet@progress.com (Mr. Usenet) Organization: Progress Software Corp. Lines: 36 Nntp-Posting-Host: ericf 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@progress.com 5 Oak Park >- Insert funny stuff here -< Bedford, MA 01730 >-Draw amusing symbols, logo's-< USA >- or characters here -<