jeff@cjsa.UUCP (C. Jeffery Small) (08/10/87)
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!---/
jgy@hropus.UUCP (John Young) (08/12/87)
> 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
avr@hou2d.UUCP (Adam V. Reed) (08/13/87)
In article <228@cjsa.UUCP>, jeff@cjsa.UUCP (C. Jeffery Small) writes: > 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. Yes it does work - check it with $ echo "${ACTION}". But you need to protect shell parameters with " " to have the shell pass them intact to a command. Incidentally, you don't need the \ before a literal newline inside single quotes. Adam Reed (hou2d!avr)
mnc@m10ux.UUCP (Michael Condict) (08/13/87)
In article <228@cjsa.UUCP>, jeff@cjsa.UUCP 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. 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' > > . . . The following dialog worked for me, using /bin/sh. Note that I use explicit new lines, rather than the "\n" notation. Also note that you should not escape a "$" character that is inside single quotes -- the escape is not interpreted specially there and will leave a literal escape character in your string, which sed will barf on: $ cmd='$a\ > dog' $ echo 'hello > goodbye' | sed -e "$cmd" The sed command appends "dog" after the last line of input, which in this case produces: hello goodbye dog as output. This is on System V Rel. 2, by the way. I think your problem is that you are mistaken about the meaning of "\n" in shell strings. It is not transformed on input to a newline character. The echo command will transform it to a new line on output, but other commands may or may not process it as a new line. To prove this to yourself, try: $ str='a\nb' $ cc "$str".c Cc will complain that it cannot open the file "a\nb.c", thus indicating that the "\n" sequence was inserted unchanged into the string and was left unchanged by the shell when it fed the string to cc. -- Michael Condict {ihnp4|vax135|cuae2}!m10ux!mnc AT&T Bell Labs (201)582-5911 MH 3B-416 Murray Hill, NJ