Path: utzoo!mnetor!tmsoft!torsqnt!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!usc!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!midway!gargoyle!chinet!les From: les@chinet.chi.il.us (Leslie Mikesell) Newsgroups: comp.editors Subject: Re: sed output Message-ID: <1991Feb25.184548.14778@chinet.chi.il.us> Date: 25 Feb 91 18:45:48 GMT References: <1991Feb20.234921.5738@ddsw1.MCS.COM> Organization: Chinet - Chicago Public Access UNIX Lines: 42 In article <1991Feb20.234921.5738@ddsw1.MCS.COM> dattier@ddsw1.MCS.COM (David W. Tamkin) writes: >Hmmm...this is knotty to explain... I'm running sed in a for loop (in sh) >on several input files. Let's call them A, B, C, D, and E. The sed script >should send output to three places. If I made a separate output file for >each one, I'd finish with fifteen output files: Ax, Ay, Az, Bx, etc., through >Ez. What I really want are a single x file, y file, and z file. >When a sed script begins, sed scans for w operators and creates all target >files needed. If files by those names already exist, tough; the old contents >are clobbered. So if I use "w x" and "w y" in the script and redirect sed's >stdout >>z, only z will have the proper contents; x and y will have only the >extractions from input file E. Simple suggestion: If the input files all exist at once, let sed iterate over the list of filenames instead of the shell. Multiple w's within a script will append. >I can use "w $i.x" and "w $i.y" (with proper quoting for the loop variable to >be expanded) and then cat [A-E].x > x and cat [A-E].y > y afterward, but I'd >rather lengthen the sed script than add the extra forks. Yes, I'm sure it >can all be done in awk or perl, but I don't know them, don't do this stuff >for a living, and certainly don't have the means or time or talent to learn >awk or perl. Ahh, but with perl, you typically only have to learn one arcane set of commands and syntax instead of the several you need to know to write useful shell scripts. If you really care about reducing the number of forks (at the expense of some start-up time), perl is the way to go. >Can sed be prevented from clobbering a file named in a w command? Probably not, but there is still a simple alternative. Just do the editting portion of the script with ex. You probably already know the appropriate syntax and it supports the w >>file command to explicitly append. As an added benifit you also get the ability to specify an address an arbitrary number of lines above a pattern match and some other things that are impossible in sed (at the expense of making a temporary copy of the file). Les Mikesell les@chinet.chi.il.us Brought to you by Super Global Mega Corp .com