The UNIX and Linux Forums  

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




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 06-30-2009
sudheer1984 sudheer1984 is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 3
sed match closest/nearest pattern

All
i am struggling to raplace some text in a line between two (closest) patterns ,

Code:
line="/home/usr/bin/:/home/usr/devuser,n1.9/bin:/home/usr/root/bin"

i want to replace "devuser,n1.9" with "NEWVAL", basically all teh text from "devuser" until nearest '/' with some new text.

i tried teh following

Code:
echo $line | sed 's/devuser.*\//NEWVAL\//'

i am getting /home/usr/bin/:/home/usr/NEWVAL/bin"

where i am expecting
/home/usr/bin/:/home/usr/NEWVAL/bin:/home/usr/root/bin"

i know sed will match the longest possible pattern, i tried this as well

Code:
echo $line | sed 's/devuser.*[^\/]/NEWVAL/'

but still no luck

any help will be appreciated.

Thanks in advance.

---------- Post updated at 02:13 AM ---------- Previous update was at 01:30 AM ----------

hey guys,
found something

Code:
echo $line | sed 's/devuser[^\/]*/NEWVAL/'

is giving me the expected output.

any other suggestions are welcome.

Last edited by otheus; 07-02-2009 at 04:59 AM.. Reason: added code tags
Bits Awarded / Charged to sudheer1984 for this Post
Date User Comment Amount
07-02-2009 otheus posting own solution 500