Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
How folks,
I've an odd situation, in sed if one wants to add a variable substitution to a sed appendage, for example:
------------------
P=456
sed "/123/ { a\
$P
}" test >test1
------------------
It replaces the $P variable correctly, but I get a "sed: command garbled: /123/ { a456"I want 456 under 123.
Am I missing a piece here?
Thanks for help....

In the past, I had a similar problem, and I couldn't get sed's append to work. I found a solution in the sed FAQ:
http://www.student.northpark.edu/pemente/sed/sedfaq.txtP=456
sed "/123/{G;s/$/${P}/;}" datafile.txtThe above works on Solaris 7 and Red Hat Linux 7.1

Neat solution - that approach never occurred to me (and works on HP-UX also). And if you want to add two lines:
P=456
Q=789
sed "/123/{G;s/$/${P}/;G;s/$/${Q}/;}" datafile.txt
But the append function works for me as below - first with just a single line, secondly appending two lines:P=456
sed -e "/123/a\\
$P" datafile.txt
P=456
Q=789
sed -e "/123/a\\
$P\\
$Q" datafile.txt

Nails,
The suggestion provided works, however, here's the other piece that does not seem to work:
#!/bin/sh -x
P=456
Str="AAAA/$P/AAAA"
sed "/123/{G;s/$/${Str}/;}" datafile.txtend result:
-----------
sed: command garbled: /123/{G;s/$/AAAA/456/AAAA/;}Is there another way for addressing $P in the Str variable string?

Lamos:
The slashes, /, which have a special meaning, are messing up the sed command. Does escaping them give you the required output:
P=456
Str="AAAA\/$P\/AAAA"
sed "/123/{G;s/$/${Str}/;}" datafile.txt

Jim,
I tried your suggestion. I tried this before minus the extra "\" and "-e" in your suggestion, which worked nicely. Also to resolve the "Str" substitution above, I added "\" before the $P variable that resolved my problem.
P=456
sed -e "/123/a\\
$P" datafile.txt
Jim/Nails thanks a bunch for your help. :-)

Jim:
Both Lamos and I missed the second \ after the append:
P=456
sed "/123/ {a\\
$P
}" datafile.txtwhich is probably why I've never been able to get sed's append to work. I'm learning sed from O'Reilly's 'sed & awk' and this text doesn't apparently show that. Would you happen to know why the second \ is required?
Off Topic: are you still considering that other issue we've emailed each other about -)
Nails

OK, consider the two echos below. The first echo has escaped the following newline character, thus eliminating it, as can be seen in the echoed result. When adding a new line, sed needs that new line to actually come in as a new separate line (or set of lines). So our added line needs to be on a line by itself, but to make sed happy, it has to continue the quoted string, so we escape the backslash with a backslash so that one backslash remains.
I don't think that was a great explanation, but the echoed results should help.
P=456
echo "sed /123/a\
$P"
sed /123/a456P=456
echo "sed /123/a\\
$P"
sed /123/a\
456On the Off Topic: Yes, I will send you some results Wednesday.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |