Bash & sed issue

Discuss Programming
Post Reply
Tux
guru
guru
Posts: 689
Joined: Wed Jan 08, 2003 10:40 am

Bash & sed issue

Post by Tux » Fri May 18, 2007 6:11 pm

I'm having trouble getting a bash script to properly evaluate a sed command, with an embedded bash variable.

What I was is for bash to call the following:

sed 's/xyz/abc$VARdef/g'

xyz is a (static) pattern to match
abc,def are arbitary (static) characters, as part of my replace
$VAR is the variable that the script needs to pass.

I've googled quickly but havn't found anything satisfactory.


TIA 8)

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Sat May 19, 2007 7:11 am

Not sure what you are trying to do but I think this is what you are trying to do:

replaceatestwith:

Code: Select all

#!/bin/bash
VAR=$1
echo "This is a test!" | sed "s/a test/not a ${VAR}!/g"
$ ./replaceatestwith drill

If you are wanting to change text in a file (or files) by passing arguments I wrote a little Perl script for that. Perl can also be used like sed, but then sed does just fine too.

The "sr" (search and replace) Perl script:
http://voidmain.is-a-geek.net/files/scripts/sr

Tux
guru
guru
Posts: 689
Joined: Wed Jan 08, 2003 10:40 am

Post by Tux » Mon May 21, 2007 5:21 pm

Thanks void, that's pretty nice!

In the end I changed the script so it doesnt use that variable :-D

Post Reply