Using Sed and Shell Variables in Multiple Lines Search and Replace using /c\

Using Sed and Shell Variables in Multiple Lines Search and Replace using /c\

Post by O » Fri, 28 Dec 2001 02:52:20



Just thought I would post this tip - didn't find it very easily
anywhere.
When using shell variables on a multiple line sed statement, you need
to use double quotes and escape the backslash:

For example, if I am doing a search and replace on the file test.txt
containing the following lines:
#BEGIN
Text to be replaced
#END
other text

and would like to replace the lines starting with #BEGIN and ending
with #END with the text contained on $REPLACEMENT_TEXT, use this sed
command:

sed "/#BEGIN/,/#END/c\\
$REPLACEMENT_TEXT
" test.txt > newfile.txt

INSTEAD OF:

sed '/#BEGIN/,/#END/c\
$REPLACEMENT_TEXT
' test.txt > newfile.txt
(which will not expand $REPLACEMENT_TEXT)

Normally, you wouldn't use double quotes, but you need to in order to
expand the variable. Without escaping the backslash at the end of the
first line sed chokes.

This is with gnu sed and using bash shell. Don't know about others.

 
 
 

Using Sed and Shell Variables in Multiple Lines Search and Replace using /c\

Post by Heiner Marx » Wed, 02 Jan 2002 12:42:55




>Just thought I would post this tip - didn't find it very easily
>anywhere.
>When using shell variables on a multiple line sed statement, you need
>to use double quotes and escape the backslash:

>For example, if I am doing a search and replace on the file test.txt
>containing the following lines:
>#BEGIN
>Text to be replaced
>#END
>other text

>and would like to replace the lines starting with #BEGIN and ending
>with #END with the text contained on $REPLACEMENT_TEXT, use this sed
>command:

>sed "/#BEGIN/,/#END/c\\
>$REPLACEMENT_TEXT
>" test.txt > newfile.txt

>INSTEAD OF:

>sed '/#BEGIN/,/#END/c\
>$REPLACEMENT_TEXT
>' test.txt > newfile.txt
>(which will not expand $REPLACEMENT_TEXT)

>Normally, you wouldn't use double quotes, but you need to in order to
>expand the variable. Without escaping the backslash at the end of the
>first line sed chokes.

Just another hint:  you can change the type of quotes like this:

sed '/#BEGIN/,/#END/c\
'"$REPLACEMENT_TEXT
" test.txt > newfile.txt

or like this:
sed '/#BEGIN/,/#END/c\'"
$REPLACEMENT_TEXT
" test.txt > newfile.txt

Both will work, and may be more readable than using double quotes for
the complete token.

Quote:>This is with gnu sed and using bash shell. Don't know about others.

This is ok for any bourne-ish shell (not csh), and for any sed I know of.
--


 
 
 

1. Search and Replace using sed and shell variable

Given the following file:

The big black cat ate the dog.
Test Location=D:\Testing

And the following script:

BUILDDIR=/scratch/Bubba

DESTFILE=JMASS.env
DESTFILETEMP=${DESTFILE}.TMP

sed 's/\(Test Location=\).*/\1\$BUILDDIR/g' $DESTFILE > $DESTFILETEMP
mv $DESTFILETEMP $DESTFILE

What I'm trying to get is a file that has the following:

The big black cat ate the dog.
Test Location=/scratch/Bubba

Instead I get:

The big black cat ate the dog.
Test Location=$BUILDDIR

2. Can't set password

3. Multi-line search and replace using sed in KSH on Solaris 5.6 box

4. .NET Licensing Protections Against Interoperability?

5. /proc/kcore -- what is it? Does reading it cause a crash?

6. wxGTK / wxWindows problems

7. find and replace multiple lines using awk or sed

8. How to search&replace multiple lines with sed?

9. Matrox Mystique ands X.

10. Search & replace date using sed

11. search & replace (using sed) only the 'n'th field.