Re: sed – using variables containing "/"
Johnny Rosenberg
gurus.knugum at gmail.com
Sun Feb 12 23:10:56 UTC 2012
2012/2/12 PleegWat <pleegwat at telfort.nl>:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 02/12/2012 07:53 PM, Johnny Rosenberg wrote:
>> So, if the variable MyVar contains a ”/”, sed will fail and a
>> workaround is to use another character instead of the ”/”
>> character for sed, like in this case, ”@”. Is this the only
>> workaround in this case or is there a more elegant one? The
>> variable ”MyVar” must not be changed in any way, other than that,
>> everything is allowed, I suppose…
>
> I'm not aware of an elegant solution in sed. You can do this in awk
> however:
>
> awk -v MyVar="${MyVar}" '{ gsub( /\$1/, MyVar ); print }' -
>
> Basically -v allows you to pass a shell variable into an awk variable,
> which removes escaping concerns.
I have realised since a while ago that awk is much more than first
meets the eye. Maybe I should invest some time in really trying to
understand it better.
Thanks for your suggestion, and thanks also to Avi who also replied.
Kind regards
Johnny Rosenberg
ジョニー・ローゼンバーグ
> PleegWat
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.11 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iQEcBAEBAgAGBQJPOEJHAAoJEAM6sLJjDJaMSgEH/A0NmgTsRdCliEuKyxb7iD/y
> 0lsHnVcujzJ0OlVaVkpBrRtlv5rbFWKQynVXn4Y8I1c9UF8FdeDY9AfEWDUo0QYd
> aLCPg9iUxDjo7UN52+nPRK8DCzsllxftK4efSA07AUhh/w8w0h03j19/vYOV5I0/
> wPTWpp/WIpnXQw15xHFLJAmK7vqhbEk9lwC+dM33mzGVxLkAEtGlnPqPqttCkaen
> dvvU7B2XFlY8Xd3wh2+2wficEethL04Q4tJTLMz6XcHM4fsdwpFV+D7wooJ4/jjh
> bl9DiBAH35AdQzWaMrJ0NTo5G1q0rsaRHHWFjo8SPFfB64SQ8I4Mx0LuQjyC1xo=
> =bWLb
> -----END PGP SIGNATURE-----
>
> --
> ubuntu-users mailing list
> ubuntu-users at lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
More information about the ubuntu-users
mailing list