LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-14-2010, 08:32 AM   #1
parajaganesh
LQ Newbie
 
Registered: May 2010
Posts: 1

Rep: Reputation: 0
How to use & in sed


Hi,

I need to do the following using sed in shell script:

tmp=(&myowntext)
sed -e "s/>\$Text</${tmp}/" tempfile1.tmp > tempfile2.tmp


But, i am getting ($Textmyowntext) as output.

Can any one help me to solve this?

-Ganesh
 
Old 05-14-2010, 08:51 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
"&" means "repeat the matched pattern". To make it literal, you would escape it---i.e. you would enter "\&"

If "&" is part of a variable, I'm not sure there is a way to do it---unless maybe you put the escape in the variable....
 
Old 05-14-2010, 09:12 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
I would probably be curious as why you would have text starting with an ampersand?
 
Old 05-14-2010, 09:32 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
I see that you put the sed expression in double quotes.

That's probably a bad idea because bash can still mangle it up unless you use single quotes (').

Anyway, "&" prints out the entire matched string, and "\&" prints an "&".
 
Old 05-14-2010, 09:34 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
If the SED expression has something like $variable in it, then you must use double-quotes.

Most SED expressions are OK with double-quotes, but all the books say to default to single-quotes and then change to double when needed.
 
Old 05-14-2010, 10:23 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
I usually prefer to switch to double quotes, like this:

Code:
'single quoted '"$var"' single quoted again'
And remember, the variable will be interpreted as a regex, not literally!
 
Old 05-14-2010, 01:01 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Ahem....
When you use something like $var to bring in the value of a variable, there is no Regex involved. The shell expands the variable and inserts the value into the command---which then runs.

As for toggling between ' and ", it is certainly OK, but has great potential for confusion. I think it is usually better to minimize the total number of characters.
 
Old 05-14-2010, 04:11 PM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by pixellany View Post
Ahem....
When you use something like $var to bring in the value of a variable, there is no Regex involved. The shell expands the variable and inserts the value into the command---which then runs.
I'm not saying that bash interprets it as a regex.

I'm saying that sed does.
 
Old 05-15-2010, 09:51 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Isn't semantics wonderful!!

A Regex is a combination of normal and "special" characters used to generalize possible text patterns. So, indeed, any combination of characters can be part of a Regex.

I read the original point as being a distinction between "literal" and "regex"

Suppose you have these two statements:

sed "s/$var/newstuff/" filename

sed "s/\$var/newstuff/" filename OR: sed 's/$var/newstuff/' filename

In both, the first stanza of the "s" construct is--by definition--a Regex. In the first, the value of var is substituted before sed does its thing. In the second, the Regex is the literal "$var"
 
Old 05-15-2010, 10:00 AM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
I knew that.
 
Old 05-15-2010, 10:32 AM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
I believe you!!---clarification is useful for other people too...

Now everyone's happy, right??.....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed & awk bret381 Linux - Software 2 12-03-2009 05:08 PM
sed s/something/multiple &/ don't get it.. crispyleif Linux - Newbie 3 02-13-2009 04:24 AM
Sed & Awk hinetvenkat Linux - Software 4 05-30-2005 06:10 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-04-2005 12:00 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM


All times are GMT -5. The time now is 03:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration