sed with variables?

Hi,

I’m using SLES10 in my company. Unfortunately, I’m struggling with passing variables to sed command. For example:

#!/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 SIGNED MESSAGE-----
Hash: SHA1

Works for me:

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

iQIcBAEBAgAGBQJKP+g6AAoJEF+XTK08PnB5T88P/3tkaNaOcuL5qL2sZhVSUp8I
ONsP0Y7jtRonVtaqT9LE1T+BUpaH28lJ2ADesB5WqhE275fA7x6dHSweNtb2smkX
IIwYYK31dhNitslFrVNGt0VaerO/pHYYVW9zZ/DGQWLI2NWARYX03K3kYmx5/nJC
j/CIuz2rcYquofDyWVZlfIOe0CbV+JLnhSAfJHJTvxEtj7Yfj7qmnUq4agN4CdS/
Ynq12lloePlZPqO+FO9gcVYDL9X8Q2S/Th8tkmoTQT/1U1ATClhzRMweFo+HFziv
/geQpIL3DMRQB02UDKEIN5nG3OqldiwBI6tV3XtbAlo8u9+VDm54FVRR6a0WZjlo
Bm8gfXaU4tj/nXGD/fw8JKULKbVNqPS+Bm2kKnW7hlHiireJpM5aP1bff1zQN0HD
mX/Hv9sBAy7YT3+uV2e3udIJrRRfMpmUDskIwKr9ycM6zTbwoUchU4Xsd+dAMBIW
Z0/HHps4ap+tun1IyeiX6C3ne5HDlYDdn0ixfbWGNTYuOtUHIpDxsbw0R7P9KvCL
WPecsKUW/Cx784a92WFfm9PaAe5btbfUHrRwE2edSZUvrEYBTbuI8IBiIjMsDhqj
c2ZroVAIKLxN0VDLts13Ht1mQbJPxMJ37/wAsOKsH64hApH4sL4qQX+D0ck0xbQ4
xwWduuprQFPKNT4oBkXf
=ZYqn
-----END PGP SIGNATURE-----

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:

Str1=search
Str2=$$A_HOST/HEX$$

sed "s/$Str1/$Str2/" test0.txt

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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

iQIcBAEBAgAGBQJKP+/9AAoJEF+XTK08PnB5qcgQAL7oozkm17NmSWFwtTM4wB9L
KqOsIRaeMg3HLM9LQzJwhC7ZWPhNuD/1SXBRxKiIvGqBiB/j0GbUhTsAvLmPhMlR
Dl0xu8xND6ZdwOqWTiiHvqJBJ1KDiWYeftgf1jaXs/+lzb/6tAAt8HceCuuNyli7
Lrhp/kJneqzwLhsOVwNrcAw6KxwO34Ig1IhcMbNdLebCNaVFIhYdL1JwKzyC2QLF
jqfwTCND/Uicifd9517Fp3aSDj/OFxqR+qBPg0Q0YaAA3an1obpj/54d6hkXSC2Q
AHy+vcL6lIWTMv9qGgtOuhdy9Bx9m+zSW5KrEM8etOGYhGcirftPb2IP93I76jMX
onabV+SG6PxBk3TT9uVZeDQiYBJx0Zd7rvd8WQDhLtdXQwahTj0GBgdYVqbadoNs
ZKzqNENqj5IfsSE2KEc5Q0RlWTj3d+qwtxXotpzu0yDy+NmNhSsirT6UI1Eel/sz
yvOpCp7Jf9q8IZUwIt9k0qRrCXkYowzqX4my6cUJcPvMTBo+YQ3w9XltbwHEBrcj
c8w8S5ReDzRlf9L5THzY+KYyiLAh8XzMGemDZ6h3hOtbyCUYSxTIzSJk3dFVFKnR
3tQ5GOB1P2A2FbqYXMUU3BjMBKtlIW2onFSzjmApxkxWolaxlYEyee9TxGguam6a
EG3l+h2QOYqg+TfFLEem
=7aeY
-----END PGP SIGNATURE-----

use awk


Str1="aaa"
Str2="bbb"
awk -v s1="$Str1" -v s2="$Str2" '{sub(s1,s2)}1' file

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.