LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-25-2011, 12:38 PM   #1
myro
LQ Newbie
 
Registered: Nov 2003
Posts: 7

Rep: Reputation: 0
Bash - String replace at the end with a regex


Hi,
I want to remove the last foldername in a string variable:
"hello/mr/robinson/"
should become
"hello/mr/"

I am trying it the following way:

a="hello/mr/robinson/"
echo ${a//[^\/]*\/$/}

But it seems that ${} does not recognize $ as end of string delimiter.
Any ideas?
 
Old 09-25-2011, 01:04 PM   #2
cheesus
Member
 
Registered: Jan 2005
Location: Munich, Germany
Distribution: SuSE
Posts: 186

Rep: Reputation: 25
Cool

Hello, how about
dirname $a
Cheers, Tom.
 
Old 09-25-2011, 01:21 PM   #3
myro
LQ Newbie
 
Registered: Nov 2003
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by cheesus View Post
Hello, how about
dirname $a
Cheers, Tom.
thanks. this works in that example, but it would still be nice to know what kind of regex are useable in ${} expressions. Also, the dirname solution seems to be slower than a ${} expression.
 
Old 09-25-2011, 01:34 PM   #4
myro
LQ Newbie
 
Registered: Nov 2003
Posts: 7

Original Poster
Rep: Reputation: 0
I also just noticed that with dirname I will get a ".", if I have "hello/" for example. But I want that to become ""
 
Old 09-25-2011, 02:50 PM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by myro View Post
Hi,
I want to remove the last foldername in a string variable:
"hello/mr/robinson/"
should become
"hello/mr/"

I am trying it the following way:

a="hello/mr/robinson/"
echo ${a//[^\/]*\/$/}

But it seems that ${} does not recognize $ as end of string delimiter.
Any ideas?
Hi,

try this:
Code:
echo ${a##${a%%*([^/])/}}
This will only work if your path does end with a slash '/'.

A few more notes:
The part in bold is called an extended pattern. You might have to set it in bash before you can use it:
Code:
shopt -s extglob
It is NOT a RegEx. Bash does not recognize RegEx within String manipulation. Therefore your '$' at the end does not work as you expected it to.

Hope this helps.
 
Old 09-25-2011, 02:59 PM   #6
myro
LQ Newbie
 
Registered: Nov 2003
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crts View Post
Hi,

try this:
Code:
echo ${a##${a%%*([^/])/}}
thanks a lot:
Code:
echo ${a%%*([^/])/}
got me the result I wanted.
 
Old 09-25-2011, 03:34 PM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Oops

Quote:
Originally Posted by myro View Post
thanks a lot:
Code:
echo ${a%%*([^/])/}
got me the result I wanted.
Somehow I misread you first post and I thought you just wanted the last part to keep. Well, glad you filtered out the useful part of my suggestion.
 
Old 09-27-2011, 04:37 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
parameter expansions don't use regular expressions, they follow shell globbing rules.

Don't forget that you can also add characters back to the string after expansion, which may help simplify things in some places. To retain the following backslash on "/hello/mr/", for example, you could simply do this:
Code:
a="hello/mr/robinson/"
echo "${a%/*/}/"

Last edited by David the H.; 09-27-2011 at 04:38 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
replace string by another string in bash shell chinhth2010 Linux - General 2 08-02-2011 10:40 PM
[SOLVED] bash regex string extraction porphyry5 Programming 2 06-10-2011 12:34 PM
bash replace all matches of regex substring in string nickleus Linux - General 3 04-30-2011 11:08 AM
Sed and regex: how to replace up to a certain string olliesa Linux - General 2 10-21-2009 02:49 AM
BASH: Replace string in file with another eur0dad Programming 5 07-27-2006 04:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration