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? |
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 Code:
% sed -e 's/[\!\/\@\#\?\$\%\^\&\*\-\+]/ /g' file Code:
% sed -e 's/[\!\/\@\#\?\$\%\^\&\*-\+]/ /g' file |
Yep, it works.
What about ' (single quote)? How should I remove single quote from text? |
Hard question :)
One way is to simply use doublequotes, like this: Code:
% cat test.txt Code:
% sed -e 's/'\\''//g' test.txt 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 :) |
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 |
Maybe is my bash.
this 6.2 RH with bash-1.14.7-23.6x_StackGuard ????? |
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 |
It works now!!
Thanks!! |
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 |
All times are GMT -5. The time now is 12:51 AM. |