ab@mybox:~> Str1=search
ab@mybox:~> Str2=nonsense
ab@mybox:~> sed “s/$Str1/$Str2/” test0.txt
The output of the file went from:
<quote>
this is a test for something to search which whoudl be
set to what is replaced. Simple: search/replace. Search
/replace. If something is searched-for it should be replaced.
</quote>
to
<quote>
this is a test for something to nonsense which whoudl be
set to what is replaced. Simple: nonsense/replace. Search
/replace. If something is nonsenseed-for it should be replaced.
</quote>
Good luck.
tisungho wrote:
> Hi,
>
> I’m using SLES10 in my company. Unfortunately, I’m struggling with
> passing variables to sed command. For example:
>
>
> Code:
> --------------------
> #!/bin/sh
>
> Str1=“aaa”
> Str2=“bbb”
>
> sed “s/$Str1/$Str2/” file1.txt > file2.txt
> --------------------
>
>
> This solution works in Ubuntu, but doesn’t work in Suse.
> Any idea?
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
Now I know why it didn’t work. I replace a string with another string which has some special characters. And of course sed doesn’t work if these characters. Do you have any idea with this string:
Str1=search
Str2=$$A_HOST/HEX$$
sed "s/$Str1/$Str2/" test0.txt
Yes… put a backslash before your forward-slash in Str2 so…
Str2=$$A_HOST/HEX$$
Good luck.
tisungho wrote:
> Hi,
>
> Now I know why it didn’t work. I replace a string with another string
> which has some special characters. And of course sed doesn’t work if
> these characters. Do you have any idea with this string:
>
>
> Code:
> --------------------
> Str1=search
> Str2=$$A_HOST/HEX$$
>
> sed “s/$Str1/$Str2/” test0.txt
> --------------------
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
On Mon, 2009-06-22 at 20:56 +0000, ab@novell.com wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Yes… put a backslash before your forward-slash in Str2 so…
>
> Str2=$$A_HOST/HEX$$
Another possible solution is to use a different delimeter in your
substiution command. For example instead of s/<whatever>/<whatever>/
you could have s@<whatever>@<whatever>@, or any character you want to
use.