Computing.Net > Forums > Unix > sed substitution appendage

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

sed substitution appendage

Reply to Message Icon

Name: lamos
Date: October 3, 2005 at 11:46:53 Pacific
OS: SUN
CPU/Ram: 2GB
Comment:

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....



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: October 3, 2005 at 13:26:37 Pacific
Reply:

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.txt

P=456
sed "/123/{G;s/$/${P}/;}" datafile.txt

The above works on Solaris 7 and Red Hat Linux 7.1



0

Response Number 2
Name: Jim Boothe
Date: October 4, 2005 at 06:30:31 Pacific
Reply:

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


0

Response Number 3
Name: lamos
Date: October 4, 2005 at 07:08:03 Pacific
Reply:

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.txt

end result:
-----------
sed: command garbled: /123/{G;s/$/AAAA/456/AAAA/;}

Is there another way for addressing $P in the Str variable string?


0

Response Number 4
Name: nails
Date: October 4, 2005 at 08:03:59 Pacific
Reply:

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


0

Response Number 5
Name: lamos
Date: October 4, 2005 at 08:12:07 Pacific
Reply:

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. :-)


0

Related Posts

See More



Response Number 6
Name: nails
Date: October 4, 2005 at 08:32:04 Pacific
Reply:

Jim:

Both Lamos and I missed the second \ after the append:

P=456
sed "/123/ {a\\
$P
}" datafile.txt

which 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


0

Response Number 7
Name: Jim Boothe
Date: October 4, 2005 at 15:29:16 Pacific
Reply:

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/a456

P=456
echo "sed /123/a\\
$P"
sed /123/a\
456

On the Off Topic: Yes, I will send you some results Wednesday.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: sed substitution appendage

Unix sed substitute text with / in www.computing.net/answers/unix/unix-sed-substitute-text-with-in-/6246.html

sed substitute '<' in XML text www.computing.net/answers/unix/sed-substitute-amplt-in-xml-text/5246.html

sed substitution www.computing.net/answers/unix/sed-substitution/7385.html