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 > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replace charandevu Shell Programming and Scripting 6 04-07-2008 11:30 AM
How to replace into Vi ravi.sadani19 Shell Programming and Scripting 5 06-25-2007 09:30 AM
Replace \n with \r\n Chanakya.m Shell Programming and Scripting 3 07-19-2006 07:20 AM
How to replace using SED? mahatma Shell Programming and Scripting 4 03-23-2006 03:04 PM
Replace jda2001 UNIX for Dummies Questions & Answers 2 09-06-2005 02:20 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-15-2008
naikaa naikaa is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 2
replace last / by |

Hi:

I want to write a Kshell script which will replace last / by |.

eg: /home/apps/test/document should be replaced as /home/apps/test|document.

The length of the string is not constant.

Thanks,
Ash
  #2 (permalink)  
Old 01-15-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,502
Code:
# echo "/home/apps/test/document" | sed 's/^\(.*\)\/\(.*\)/\1\|\2/'
/home/apps/test|document

Last edited by ghostdog74; 01-15-2008 at 05:38 AM..
  #3 (permalink)  
Old 01-15-2008
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,206
Code:
sed 's_/\([^/]+\)$_|\1_'
  #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: 704
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
  #5 (permalink)  
Old 01-15-2008
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,206
Actually, what I intended to post was this, but got distracted by an awk script:

Code:
sed 's_/\([^/][^/]*\)$_|\1_'
If you mean something followed by at least one other character, be explicit.
  #6 (permalink)  
Old 01-15-2008
V3l0 V3l0 is offline
Registered User
  
 

Join Date: Nov 2007
Location: Belgium & France
Posts: 70
Code:
cr=$(echo /tutu/tatat/titi/tutu | awk -F/ '{ print $NF; }')
echo /tutu/tatat/titi/tutu | sed -e 's/\/'$cr'$/|'$cr'/'
/tutu/tatat/titi|tutu
  #7 (permalink)  
Old 01-17-2008
naikaa naikaa is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 2
Hi guyz:

Thank you for your help. Esp Reborg, i used your suggestion, and it works perfectly well.

Thanks!
Sponsored Links
Closed Thread

Bookmarks

Tags
regex, regular expressions

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 08:52 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