Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!ames!amdcad!sun!rdh From: rdh@sun.UUCP Newsgroups: comp.unix.wizards Subject: shell and sed conflicts (was embedded NL's in shell vars) Message-ID: <25712@sun.uucp> Date: Sat, 15-Aug-87 02:33:22 EDT Article-I.D.: sun.25712 Posted: Sat Aug 15 02:33:22 1987 Date-Received: Sun, 16-Aug-87 09:08:20 EDT References: <228@cjsa.UUCP> Reply-To: rdh@sun.UUCP (Robert Hartman) Organization: Sun Microsystems, Mountain View Lines: 53 Keywords: sed sh shell In article <228@cjsa.UUCP> jeff@cjsa.UUCP (C. Jeffery Small) writes: >I have been attempting to construct a 'sed' command line script and load it >into a shell variable (called ACTION) for later use within a bourne shell >script. ... [ describes problem passing a NEWLINE to sed via shell variable ] While it may be inelegant in principle, a hack I often use is simply to build a temporary sed script in a separate file, and then run sed -f with that script. This avoids many such conflicts between sed and the shell. sed ... > /tmp/$file # Build a temporary sed script echo "/$address/a\\" > $sedscript echo "$appendln" >> $sedscript # Apply temporary sed script sed -f $sedscript /tmp/$file > $outfile rm -f /tmp/$file $sedscript It's gross, but it works. If you can't stand the thought of having TWO temporary files, a somewhat more elegant way is to use a subshell: sed ... | \ ( echo "/$address/a\\" > $sedscript ; \ echo "$appendln" >> $sedscript \ ) | sed -f $sedscript > $outfile ; rm -f $sedscript But, I've had only mixed success with this. When a shell script gets this complicated, I tend to opt for temp files. I'd rather waste a little temp space than have to debug my way thru 2 shells and 2 complex seds. I wholeheartedly agree with anyone who is appalled by this kludge, but after all, we're talking about shell scripts, hopefully for personal or limited use. I would never suggest that anyone do this in production scripts. You'll undoubtedly get a million suggestions to "just use awk," but I don't know. I don't see why one should have to learn yet another programming language (with less-flexible RE handling), to implement knock-off tasks that are already easy to think about in terms of the regular editing commands common to ed, ex, sed, and vi (and the already-familiar shell control constructs). A "real" solution to this problem would be to teach sed to recognize SOME PRINTABLE ESCAPE SEQUENCE as an embedded NEWLINE. By the way, has anyone come up with a better way to get sed to give you "just the line after `address'" than: ... | sed -n -e "/addr/N" -e "/addr.*/p" | sed "/addr/d" -bob.