rdh@sun.UUCP (08/15/87)
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.
chris@mimsy.UUCP (Chris Torek) (08/17/87)
There is no problem embedding newlines in shell variables. The problems people have experienced are with *evaluation* of shell variables that *contain* newlines. The difference is that between putting it in and pulling it out again. (Did you really just think what I think you thought? :-) ) For instance: #! /bin/sh foo='bar baz' echo $foo echo "$foo" produces bar baz bar baz because the shell's IFS (internal field separator) set includes newlines. The unquoted $foo expands to two arguments. #! /bin/sh foo='bar baz' IFS=' ' # that is space and tab echo $foo produces bar baz because newline has been removed from the shell's field separator set, so the unquoted $foo expands to a single argument. Read carefully the description of variable substitution under `man 1 sh'. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) Domain: chris@mimsy.umd.edu Path: seismo!mimsy!chris