The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sort command - strange behaviour miwinter UNIX for Advanced & Expert Users 16 05-29-2008 04:19 AM
Longer commands and strange behaviour on ksh anurags UNIX for Dummies Questions & Answers 2 03-27-2008 08:04 AM
A Strange Behaviour!!! navojit dutta Shell Programming and Scripting 5 12-21-2007 01:35 AM
/etc/passwd strange behaviour! penguin-friend Linux 0 06-06-2005 09:00 AM
very strange behaviour on unix server bolo77 UNIX for Dummies Questions & Answers 7 03-02-2005 12:32 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-07-2008
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Strange sed behaviour

Code:
[/tmp]$ echo a.bc | sed -e "s/\|/\\|/g"
|a|.|b|c|
[/tmp]$
Is the behavior of the sed statement expected ? Or is this a bug in sed ?

OS details

Code:
Linux 2.6.9-55.0.0.0.2.ELsmp #1 SMP Wed May 2 14:59:56 PDT 2007 i686 i686 i386 GNU/Linux
Reply With Quote
Forum Sponsor
  #2  
Old 02-07-2008
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,263
The problem is perhaps in the quotation you use: The shell is probably "eating" your escape chars away and sed doesn't see what you expect it to see.

I made it a habit to use always single quotes for sed-statements to avoid this. It is even possible to use single quotes when using a variable inside an sed-statement:

sed 's/'"$src"'/'"$tgt"'/g'

will change ocurrences of $src to the value of $tgt

I hope this helps.

bakunin
Reply With Quote
  #3  
Old 02-07-2008
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Fine. The single quotes vs double quotes has an impact.

Code:
[/tmp]$ echo a.bc | sed -e "s/\|/\\|/g"
|a|.|b|c|
[/tmp]$ echo a.bc | sed -e 's/\|/\\|/g'
\|a\|.\|b\|c\|
[/tmp]$
But that does not explain those extra characters in the output. In either case I would expect the output to be a.bc and not anything which has | or \| wrapped around every character.

Am I missing something something here ?
Reply With Quote
  #4  
Old 02-07-2008
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,263
Quote:
Originally Posted by vino View Post
Am I missing something something here ?
To be honest, now i'm astonished myself:

lacking a UNIX machine (got a day off) i fired up cygwin and tried:

Code:
# echo a.bc | sed --posix 's/\|/x/g'
xax.xbxcx

# echo 'a.bc' | sed --posix 's/\|/x/g'
xax.xbxcx

# echo 'a.bc' | sed --posix 's/|/x/g'
a.bc

# sed --version
GNU sed version 4.1.5
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, to the extent permitted by law.
It seems that "\|" is matching the NULL-regexp, whereas "|" is matching a single pipe character as expected. I have no idea, why this is the case, but will investigate further.

bakunin
Reply With Quote
  #5  
Old 02-07-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,580
my guess:
\| is used in sed (gnu) as alternation. therefore
Code:
# echo "a.bc" | sed -e 's/\|/\\|/g'
\|a\|.\|b\|c\|
seem to say "blank" or "blank" (or null?) substitute with \|, hence the result.
if really want to search for a "|", use the open square brackets
Code:
# echo "a.bc" | sed -e 's/[|]/\\|/g'
a.bc
# echo "a|bc" | sed -e 's/[|]/\\|/g'
a\|bc
Reply With Quote
  #6  
Old 02-07-2008
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Quote:
Originally Posted by ghostdog74 View Post
my guess:
\| is used in sed (gnu) as alternation. therefore
Code:
# echo "a.bc" | sed -e 's/\|/\\|/g'
\|a\|.\|b\|c\|
seem to say "blank" or "blank" (or null?) substitute with \|, hence the result.
if really want to search for a "|", use the open square brackets
Code:
# echo "a.bc" | sed -e 's/[|]/\\|/g'
a.bc
# echo "a|bc" | sed -e 's/[|]/\\|/g'
a\|bc
Wouldn't the alternation operator require atleast two operands ?
Reply With Quote
  #7  
Old 02-08-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/fl
Posts: 1,061
Humm, seems to be a GNU sed-ism

Under Interix 6.0 and ksh-93 and the OOB sed (non-GNU), it works as expected

Code:
$ echo a.bc | sed -e "s/\|/\\|/g"
a.bc
$
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:32 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0