LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-22-2012, 04:47 AM   #1
seismo18
LQ Newbie
 
Registered: Jul 2011
Posts: 10

Rep: Reputation: Disabled
how to replace '$' from a file using sed command


I have text file in which, I want to replace '$' by $.
I am trying:
sed "s/'$'/$/g" test2

It shows illegal variable name.
I have tryed in this way also, sed "s/\'$\'/$/g" test2
and some other combination. But nothing work.

Can you suggest me how to replace '$' by $.
Thanking you.
 
Old 11-22-2012, 05:03 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
The first one should work. Are you sure you copied it correctly?
 
Old 11-22-2012, 05:09 AM   #3
seismo18
LQ Newbie
 
Registered: Jul 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Ya I have tried it correctly. But it did not work.
When I am trying:
sed "s/'>'/>/g" test2, then it working.
This I have using for the same file.
But it don't work for:
sed "s/'$'/$/g" test2
 
Old 11-22-2012, 05:22 AM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
try escaping those '$':
Code:
sed "s/'\$'/\$/g" test2
 
Old 11-22-2012, 05:32 AM   #5
seismo18
LQ Newbie
 
Registered: Jul 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
It did not work.
Now it showing error as "variable name must contain alphanumeric characters".
 
Old 11-22-2012, 05:37 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
oh. Are you using (t)csh by any chance? If so, this may be a bit problematic. Anyway, while ugly, this should work:
Code:
 sed s/"'"'$'"'"'/$/g' test2

Last edited by millgates; 11-22-2012 at 05:38 AM.
 
1 members found this post helpful.
Old 11-22-2012, 05:55 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
In BASH:
Code:
sed 's/'\''\$'\''/\$/g'
translation (after the s): turn off hard quotes, then insert literal ', then turn on hard quotes again, then insert literal $. finally, repeat the first sequence to get another literal '

I still don't totally understand it, but I got the hint here: http://www.grymoire.com/Unix/Quote.html
 
3 members found this post helpful.
Old 11-22-2012, 06:00 AM   #8
seismo18
LQ Newbie
 
Registered: Jul 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks it working.
Ya I am using tcsh.
Yes in bash shell, sed "s/'$'/$/g" work smoothly. I have check just know.
But still I did not understand why it was working for '>' in tcsh but did not work for '$'. And why you have use so many seperater.

Is bash shell is convinent than (t)csh.
 
Old 11-22-2012, 06:07 AM   #9
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
It doesn't work because tcsh sees the '$' character and tries to substitute it with a variable. That's why the dollar sign has to be within single quotes. The single quotes, on the other hand, have to be within double quotes. Escaping neither $ nor ' seems to work in csh. But I don't use csh, so I don't know... maybe there's a better solution. I allways thought quoting and escaping would work the same way in all unix shells. Seems I was wrong
 
1 members found this post helpful.
Old 11-22-2012, 11:15 AM   #10
mmoreno80
LQ Newbie
 
Registered: Nov 2012
Posts: 8

Rep: Reputation: Disabled
Millgates, quoting an escaping doesn't work the same way in all unix shells.

Both csh and tcsh are known because of their weird behaviour ... take a look at http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ Section 4: QUOTING.
 
  


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
Replace a line in file used sed command vin_2 Programming 1 10-17-2012 02:04 AM
Replace string using sed command bloodstreetboy Linux - General 6 07-06-2012 03:03 PM
[SOLVED] sed command to replace file extension leighya Linux - Newbie 18 04-22-2012 03:20 AM
[SOLVED] how we can find and replace multiple file using sed command jayakumar01 Linux - Server 1 04-05-2012 07:53 AM
sed command to replace line in text file gengwei89 Linux - Newbie 6 11-06-2011 08:10 AM


All times are GMT -5. The time now is 07:10 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration