FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Ubuntu > Ubuntu User

 
 
LinkBack Thread Tools
 
Old 02-12-2012, 09:50 PM
PleegWat
 
Default sed – using variables containing "/"

-----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.

PleegWat
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJPOEJHAAoJEAM6sLJjDJaMSgEH/A0NmgTsRdCliEuKyxb7iD/y
0lsHnVcujzJ0OlVaVkpBrRtlv5rbFWKQynVXn4Y8I1c9UF8Fde DY9AfEWDUo0QYd
aLCPg9iUxDjo7UN52+nPRK8DCzsllxftK4efSA07AUhh/w8w0h03j19/vYOV5I0/
wPTWpp/WIpnXQw15xHFLJAmK7vqhbEk9lwC+dM33mzGVxLkAEtGlnPqPq ttCkaen
dvvU7B2XFlY8Xd3wh2+2wficEethL04Q4tJTLMz6XcHM4fsdwp FV+D7wooJ4/jjh
bl9DiBAH35AdQzWaMrJ0NTo5G1q0rsaRHHWFjo8SPFfB64SQ8I 4Mx0LuQjyC1xo=
=bWLb
-----END PGP SIGNATURE-----

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 02-12-2012, 10:10 PM
Johnny Rosenberg
 
Default sed – using variables containing "/"

2012/2/12 PleegWat <pleegwat@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
> 0lsHnVcujzJ0OlVaVkpBrRtlv5rbFWKQynVXn4Y8I1c9UF8Fde DY9AfEWDUo0QYd
> aLCPg9iUxDjo7UN52+nPRK8DCzsllxftK4efSA07AUhh/w8w0h03j19/vYOV5I0/
> wPTWpp/WIpnXQw15xHFLJAmK7vqhbEk9lwC+dM33mzGVxLkAEtGlnPqPq ttCkaen
> dvvU7B2XFlY8Xd3wh2+2wficEethL04Q4tJTLMz6XcHM4fsdwp FV+D7wooJ4/jjh
> bl9DiBAH35AdQzWaMrJ0NTo5G1q0rsaRHHWFjo8SPFfB64SQ8I 4Mx0LuQjyC1xo=
> =bWLb
> -----END PGP SIGNATURE-----
>
> --
> ubuntu-users mailing list
> ubuntu-users@lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 02-19-2012, 12:50 PM
Johnny Rosenberg
 
Default sed – using variables containing "/"

2012/2/12 Avi Greenbury <lists@avi.co>:
> 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, ”@”.
>
> Yeah, rather than s/4/3/four-thirds/ one might do s@4/3@four-thirds@
>
>> 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…
>
> If you can't escape it, then yeah that's all you can do in sed as far
> as I'm aware. That doesn't strike me as particularly inelegant
> (especially considering how much I use sed for changing filepaths) - is
> there some other reason to not do it that way?

Well, maybe the variable in some rare cases contains one or more ”@”?

>
> If you want to be more flexible, though, and so permit all characters
> in the strings (or at least not need to predict what might be in them)
> it would probably be easiest to pipe it into a perl one-liner.
>
> --
> Avi.
>
> --
> ubuntu-users mailing list
> ubuntu-users@lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 

Thread Tools




All times are GMT. The time now is 04:40 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org