The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 01-15-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 717
Hi.
Quote:
Originally Posted by reborg View Post
Code:
sed 's_/\([^/]+\)$_|\1_'
I needed to use "*" instead of "+". Some versions of sed may need "-r" to use extended regular expressions, in which the parens also need to modified:

Code:
#!/bin/sh -

# @(#) user2    Demonstrate replacement of last specific character, sed.

sed --version | head -1

echo
echo " Original:"
echo "/home/apps/test/document" |
sed 's_/\([^/]+\)$_|\1_'

echo
echo " Modified:"
echo "/home/apps/test/document" |
sed 's_/\([^/]*\)$_|\1_'

echo
echo " Original with -r:"
echo "/home/apps/test/document" |
sed -r -e 's_/([^/]+)$_|\1_'

exit 0

Producing:

Code:
% ./user2
GNU sed version 4.1.2

 Original:
/home/apps/test/document

 Modified:
/home/apps/test|document

 Original with -r:
/home/apps/test|document

There are (too) many versions of sed, so one must know which version one is using. For example, if you were to leave the "\(", "\)" in the 3rd example, you would get an error message, "invalid reference \1", not very informative, at least not directly ... cheers, drl