LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-31-2008, 11:35 PM   #1
Ammad
Member
 
Registered: Apr 2004
Distribution: redhat 9.0, fc4, redhat as 4
Posts: 519

Rep: Reputation: 31
regular expression


Dear all,

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

thanks
 
Old 08-01-2008, 12:22 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Quote:
Originally Posted by Ammad View Post
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.

Last edited by pixellany; 08-01-2008 at 08:42 AM.
 
Old 08-01-2008, 01:23 AM   #3
Ammad
Member
 
Registered: Apr 2004
Distribution: redhat 9.0, fc4, redhat as 4
Posts: 519

Original Poster
Rep: Reputation: 31
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.
 
Old 08-01-2008, 02:22 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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.
 
Old 08-01-2008, 04:01 AM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
In sed "\\" represents a literal "\"
Code:
echo "this is\ test" | sed 's/\\/windows/'
this iswindows test
 
Old 08-01-2008, 08:41 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
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.
 
  


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
Regular Expression msvinaykumar Programming 2 08-14-2006 09:48 AM
Need help with regular expression aecaudel Programming 6 11-04-2005 06:28 AM
Need help with Regular Expression subaruwrx Linux - Newbie 6 09-04-2004 08:48 PM
Regular Expression slizadel Programming 4 07-28-2003 06:16 AM
regular expression gumby Programming 3 07-15-2003 01:13 PM


All times are GMT -5. The time now is 11:49 PM.

Main Menu
Advertisement
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