The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

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 07:19 AM
Longer commands and strange behaviour on ksh anurags UNIX for Dummies Questions & Answers 2 03-27-2008 11:04 AM
A Strange Behaviour!!! navojit dutta Shell Programming and Scripting 5 12-21-2007 04:35 AM
/etc/passwd strange behaviour! penguin-friend Linux 0 06-06-2005 12:00 PM
very strange behaviour on unix server bolo77 UNIX for Dummies Questions & Answers 7 03-02-2005 03:32 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-07-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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
  #2 (permalink)  
Old 02-07-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
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
  #3 (permalink)  
Old 02-07-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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 ?
  #4 (permalink)  
Old 02-07-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
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
  #5 (permalink)  
Old 02-07-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,513
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
  #6 (permalink)  
Old 02-07-2008
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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 ?
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0