LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 07-26-2005, 12:59 PM   #1
zivota
Member
 
Registered: May 2005
Distribution: CENTOS
Posts: 91

Rep: Reputation: 15
SED question


Why this this command doesn't replace dash (-) with empty space

`sed -e 's/[\!\/\@\#\?\$\%\^\&\*\-\+]/ /g' file1 > file2`

For some reason I still have "-" in file2.
If I do only dash replacement if will work.

`sed -e 's/-/ /g' file1 > file2

Any idea?
 
Old 07-26-2005, 02:54 PM   #2
stefanlasiewski
Member
 
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92

Rep: Reputation: 16
You don't need to escape the dash character.

Sed is interpreting the escaped dash character as a special character I think.

You don't need to escape the dash character.

Change the '\-' to just a '-':

Here's the file:
Code:
% cat file
foo-bar
foo!bar
Using '\-' doesn't work:
Code:
% sed -e 's/[\!\/\@\#\?\$\%\^\&\*\-\+]/ /g' file
foo-bar
foo bar
Using '-' does work:
Code:
% sed -e 's/[\!\/\@\#\?\$\%\^\&\*-\+]/ /g' file
foo bar
foo bar
 
Old 07-26-2005, 04:26 PM   #3
zivota
Member
 
Registered: May 2005
Distribution: CENTOS
Posts: 91

Original Poster
Rep: Reputation: 15
Yep, it works.

What about ' (single quote)?
How should I remove single quote from text?
 
Old 07-26-2005, 05:05 PM   #4
stefanlasiewski
Member
 
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92

Rep: Reputation: 16
Hard question

One way is to simply use doublequotes, like this:

Code:
% cat test.txt
Don't taunt happy-fun ball.
%  sed -e "s/'/ /g" test.txt
Dont taunt happy-fun ball.
But if you cannot enclose the sed pattern in double quotes, you need to use some quoting tricks, like this:

Code:
% sed -e 's/'\\''//g' test.txt
Dont taunt happy-fun ball.

The special part is the '\''. The first single-quote in that statement closes the first single-quoted section, the escaped single-quote \' prints an single-quote, and the third single-quote starts a new single-quoted section.

I finally found the answer in the SED faq: http://www.faqs.org/faqs/editor-faq/sed/ , question "4.10. How do I handle shell quoting in sed?"

Tuck that piece of knowledge away somewhere. I run into this every couple of years and quickly forget

Last edited by stefanlasiewski; 07-26-2005 at 06:05 PM.
 
Old 07-26-2005, 05:32 PM   #5
zivota
Member
 
Registered: May 2005
Distribution: CENTOS
Posts: 91

Original Poster
Rep: Reputation: 15
Hmmmmmm,

I Do not see double quotes here, only single quotes around single quote. Is that what you thought.

>Don't taunt happy-fun ball.
>% sed -e 's/'''//g' test.txt

# cat test
0563890'01817,NI B-IN-ERS - 4MM,1.51,3508,NO PROMO

sed -e 's/'''/ /g' test

....gave me

>

...and waiting for input
 
Old 07-26-2005, 05:35 PM   #6
zivota
Member
 
Registered: May 2005
Distribution: CENTOS
Posts: 91

Original Poster
Rep: Reputation: 15
Maybe is my bash.

this 6.2 RH with
bash-1.14.7-23.6x_StackGuard

?????
 
Old 07-26-2005, 05:54 PM   #7
stefanlasiewski
Member
 
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92

Rep: Reputation: 16
Sorry about that. I pasted the wrong example up above. I fixed it.

In regards to the doublequotes solution, try this syntax, where we use doublequotes around the entire s///g statement:

Code:
sed -e "s/'/ /g" test.txt

Last edited by stefanlasiewski; 07-26-2005 at 06:04 PM.
 
Old 07-26-2005, 06:05 PM   #8
zivota
Member
 
Registered: May 2005
Distribution: CENTOS
Posts: 91

Original Poster
Rep: Reputation: 15
It works now!!

Thanks!!
 
Old 07-26-2005, 06:07 PM   #9
stefanlasiewski
Member
 
Registered: Aug 2003
Location: Berkeley, California, USA
Distribution: Red Hat Enterprise Linux, Debian & Ubuntu
Posts: 92

Rep: Reputation: 16
And I just noticed that the other example was munged by these forums. I fixed the example above, but here it is again.

Here it is again. Notice that the second single quote is escaped with a backslash, like \\' :

Code:
% sed -e 's/'\\''//g' test.txt
Dont taunt happy-fun ball.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed question ckoniecny Linux - General 3 11-11-2005 09:31 AM
sed question sphynx Programming 6 04-28-2004 06:25 PM
little sed question freelinuxcpp Linux - Software 3 01-20-2004 07:36 AM
sed question zoomzoom Linux - Software 2 10-20-2003 04:04 PM
A question on 'sed' Barbarian Programming 4 04-11-2002 10:19 PM


All times are GMT -5. The time now is 02:32 AM.

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