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, 05:53 PM
Johnny Rosenberg
 
Default sed – using variables containing "/"

Text in terminal below as typed, except text inside [], which can be
either a keyboard short-cut or a comment.

––– Terminal input/output–––
MyPrompt $ cat > testfile
Replace the next line with something:
$1
Here is another line, don't replace it.
[Ctrl+d]
MyPrompt $ MyVar="/"
MyPrompt $ echo $MyVar
/
MyPrompt $ cat testfile | sed -e "s/$1/$MyVar/"
sed: -e uttryck #1, tecken 7: flaggan okänd för "s" [In English,
something like: ”sed: -e expression #1, character 7: flag unknown for
"s"”]
MyPrompt $ cat testfile | sed -e "s@$1@$MyVar@"
Replace the next line with something:
/
Here is another line, don't replace it.
MyPrompt $
––– End of terminal input/output –––

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…


Kind regards

Johnny Rosenberg
ジョニー・*ーゼンバーグ

--
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, 06:22 PM
Avi Greenbury
 
Default sed – using variables containing "/"

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?

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
 

Thread Tools




All times are GMT. The time now is 01:33 PM.

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