Discussion:
Need help with reading in a fileto replace a line, using sed
d***@comcast.net
2013-03-04 18:51:34 UTC
Permalink
Hi.  I have a shell script that uses ed to replace a line in file textfile1

containing the pattern xxxxx with the contents of a file (textfile2).

It does this by locating the line, deleting the line, then reading in textfile2.

The only concern with textfile2 is that it might also contain the same pattern (xxxxx).

textfile1 will only have this pattern (xxxxx) occur once.



I am wondering if someone knows a way to do this same thing using sed.

Remember that textfile2 might also contain the pattern xxxxx.

Thanks for any help.


#!/bin/sh

cp textfile1.sav textfile1

pattern="xxxxx"

ed textfile1 <<- ENDEDIT 1>/dev/null 2>&1
        1
        /xxxxx
        d
        .r textfile2
        w
        q
ENDEDIT


[Non-text portions of this message have been removed]
Davide Brini
2013-03-04 20:41:51 UTC
Permalink
Post by d***@comcast.net
Hi.  I have a shell script that uses ed to replace a line in file textfile1
containing the pattern xxxxx with the contents of a file (textfile2).
It does this by locating the line, deleting the line, then reading in textfile2.
The only concern with textfile2 is that it might also contain the same pattern (xxxxx).
textfile1 will only have this pattern (xxxxx) occur once.
I am wondering if someone knows a way to do this same thing using sed.
Remember that textfile2 might also contain the pattern xxxxx.
The (imho) weird behavior of "r" in sed is actually useful here:

sed -e '/xxxxx/{ r textfile2' -e 'd; }' textfile1
--
D.
d***@comcast.net
2013-03-04 21:46:48 UTC
Permalink
Thanks.  That's great solution!

----- Original Message -----
Post by d***@comcast.net
Hi.  I have a shell script that uses ed to replace a line in file
textfile1
containing the pattern xxxxx with the contents of a file (textfile2).
It does this by locating the line, deleting the line, then reading in
textfile2.
The only concern with textfile2 is that it might also contain the same
pattern (xxxxx).
textfile1 will only have this pattern (xxxxx) occur once.
I am wondering if someone knows a way to do this same thing using sed.
Remember that textfile2 might also contain the pattern xxxxx.
The (imho) weird behavior of "r" in sed is actually useful here:

sed -e '/xxxxx/{ r textfile2' -e 'd; }' textfile1
--
D.




[Non-text portions of this message have been removed]
Loading...