LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   SED question (https://www.linuxquestions.org/questions/linux-general-1/sed-question-346989/)

zivota 07-26-2005 12:59 PM

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?

stefanlasiewski 07-26-2005 02:54 PM

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


zivota 07-26-2005 04:26 PM

Yep, it works.

What about ' (single quote)?
How should I remove single quote from text?

stefanlasiewski 07-26-2005 05:05 PM

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 :)

zivota 07-26-2005 05:32 PM

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

zivota 07-26-2005 05:35 PM

Maybe is my bash.

this 6.2 RH with
bash-1.14.7-23.6x_StackGuard

?????

stefanlasiewski 07-26-2005 05:54 PM

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

zivota 07-26-2005 06:05 PM

It works now!!

Thanks!!

stefanlasiewski 07-26-2005 06:07 PM

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.



All times are GMT -5. The time now is 12:51 AM.