Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!gatech!hao!oddjob!gargoyle!ihnp4!homxb!houxm!hropus!jgy From: jgy@hropus.UUCP (John Young) Newsgroups: comp.unix.wizards Subject: Re: embedded newlines in shell variable? Message-ID: <1243@hropus.UUCP> Date: Wed, 12-Aug-87 16:44:20 EDT Article-I.D.: hropus.1243 Posted: Wed Aug 12 16:44:20 1987 Date-Received: Sat, 15-Aug-87 09:42:01 EDT References: <228@cjsa.UUCP> Organization: Bell Labs, Holmdel, NJ Lines: 58 Keywords: sed sh shell > 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. All goes well until I need sed to append new text to the end of the > input data. To work, sed's append command ( a\ ) requires newlines to be > embedded as follows: > '$a\ > NEW LINE OF MATERIAL' > > > What follows is an example of what I am attempting to achieve: > > Match last line ------++-----Append text > || > vv > ACTION=" -e '\$a\\nNEW LINE OF MATERIAL'" > ... > sed ${ACTION} $1 > > > My experience has been that using '\n' in the assignment get the newline > into the variable so that echo $ACTION prints correctly, but sed will > not interpret the '\n' sequence properly. > > On the other hand, attempting to embed actual newlines (^J) within the > assignment (ie. ACTION="-e '\$a\^JNEW LINE OF MATERIAL'") does not work > since the shell substitutes the ^J with a single space. > > I have tried to escape every character in sight and even attempted an > eval on $ACTION under various modes of construction, without success. > > I'm guessing that there is a simple (and obvious) trick for doing what I > want. Any pointers would be greatly appreciated. > > Thanks in advance. > ---- > Jeffery Small (203) 776-2000 UUCP: ihnp4!---\ > C. Jeffery Small and Associates hsi!cjsa!jeff > 123 York Street, New Haven, CT 06511 hao!noao!---/ > Try this: # action="\$a\\ hello\\ world" # date | sed -e "$action" You need to get the a single backslash thru to sed prior to each end of line. I hope this isn't all you want, because if so: # echo "NEW LINE OF MATERIAL" >> file or # { cat file echo "NEW LINE OF MATERIAL" } would be much edididor