LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   regular expression (https://www.linuxquestions.org/questions/linux-general-1/regular-expression-659636/)

Ammad 07-31-2008 10:35 PM

regular expression
 
Dear all,

Is it possible to replace "\" with "blank space" using sed.

thanks

pixellany 07-31-2008 11:22 PM

Quote:

Originally Posted by Ammad (Post 3232745)
Dear all,

Is it possible to replace "\" with "blank space" using sed.

thanks

Yes!!
.
Oh!!!--did you mean to ask HOW??

The trick here is that any character can be used as the separator in the SED "s" (substitute) construct. So, just use this general form:
sed 's#old#new#' (add a "g" to make it replace all "old" with "new".) old or new can now contain the "/".

The other method is to escape the "/" so that it is not interpreted as a separator. eg use "\/" to get a literal "/".

OOPS--I read "/" instead of "\"---see below.

Ammad 08-01-2008 12:23 AM

I used the following,


linux ~]# echo "this is\ test" | sed 's#\#windows#'
sed: -e expression #1, char 12: unterminated `s' command


linux ~]# echo "this is\ test" | sed 's#test#windows#'
this is\ windows


the first one outputs error, whereas if any other charcter it replaces successfuly. :(

ghostdog74 08-01-2008 01:22 AM

You can use awk. make the input delimiter as "\" , then set output field delimiter as null
Code:

# echo "this is\ test" | awk -F'\' '{$1=$1;}1' OFS=""
this is test

minimal regular expression.

Kenhelm 08-01-2008 03:01 AM

In sed "\\" represents a literal "\"
Code:

echo "this is\ test" | sed 's/\\/windows/'
this iswindows test


pixellany 08-01-2008 07:41 AM

Sorry--I misread the original post.

Any character which normally has a special meaning has to be escaped. "/" only has special meaning in the "s" command when it is used as the delimiter. "\" is always special unless escaped.


All times are GMT -5. The time now is 08:16 AM.