General
cancel
Showing results for 
Search instead for 
Did you mean: 

Using regular expressions with sed

SOLVED
Go to solution
Raynald Boucher
Super Advisor

Using regular expressions with sed

Hello all,

I'm trying to write a script that will remove a directory from the PATH environment variable.
I know the directory (MF_SE) but not which tree it's in (ie it could be under a users /home or in /usr/local or elsewhere.
So I'm trying to replace ":[a-z/]MF_SE[a-z/]:" by ":"

I tried the following without results:
sed -e 's|:\[a-z/\]MF_SE\[a-z/\]:|:|' path
...:/usr/lib/nis:/opt/sec_mgmt/spc/bin:/opt/ssh/bin:/opt/oracle/product/10g/bin:/home/boucherr/MF_SE_41/history/bin:.:/home/boucherr/bin:.

As you see, no warnings or errors but no change performed.

Can anyone shed light?
Thanks
5 REPLIES
Hein van den Heuvel
Honored Contributor
Solution

Re: Using regular expressions with sed

What if there are uppercase characters or numerics in the path?

Me thinks you want to find a ':', any number of not-':', a slash, the directory name, any number of not-':, and a ':'.

As in:

PATH=$( echo $PATH | sed -e 's|:[^:]*/MF_SE[^:]*:|:|' )


hth,
Hein.
Raynald Boucher
Super Advisor

Re: Using regular expressions with sed

Way to go!

I had made some corrections to mt original attempt and got it working with an uppercase identification string but could not get results with a lowercase string.

I didn't know about the "not" character.
Where is this documented?
I checked the man pages for sed, vi, regexp and I didn't spot it.

Many thanks

RayB
James R. Ferguson
Acclaimed Contributor

Re: Using regular expressions with sed

Hi Raynald:

> I didn't know about the "not" character.
Where is this documented?

See 'man 5 regexp' in the discussion of "non-matching list".

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: Using regular expressions with sed

I know it from the perl man pages, but I just looked it up.
man sed --> man regexp
man 5 regexp --->

" ^ The circumflex is special when used as the first character of an entire RE (see Expression Anchoring) or as the first character of a bracket expression."

" non-matching list
A non-matching list expression begins with a circumflex (^), and specifies a list that matches any character or collating element except newline and the characters represented in the list."


hth,
Hein.


Raynald Boucher
Super Advisor

Re: Using regular expressions with sed

Good, I'll look it up more attentively.
I had used "man regexp".

Thanks again

RayB