You are not logged in.

#1 2012-03-05 20:18:30

Prowler
Member
Registered: 2011-05-16
Posts: 16

Replacing text between backslashes

Hi, recently ive been working on a file backup script of mine (sorry if I am missing on an easier way for that). I have a list of files in an array:
gg[1]='/home/discharge/.bashrc'

I want to make a variable that takes gg[1] and removes everything until the name of the file itself is left. So what I need is

gg[1]='/home/discharge/.bashrc'
shorter[1]='.bashrc'

How to automate so that i can extract only that last part? Tried "sed", but after an hour of trying, I'm giving up on that.
Any ideas appreciated smile

Offline

#2 2012-03-05 20:26:37

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Replacing text between backslashes

$basename /home/discharge/.bashrc
.bashrc
 $sed 's,/.*/,,' <<< '/home/discharge/.bashrc'
.bashrc

smile

Edit, alphaniner's solution is probably best though. I keep forgetting about pe. neutral

Last edited by skanky (2012-03-05 20:31:27)


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#3 2012-03-05 20:27:57

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: Replacing text between backslashes

Use bash parameter expansion for that!

${parameter##pattern}

so

if ${gg[1]} is '/home/discharge/.bashrc'
then ${gg[1]##*/} would be '.bashrc'

See Greg's Wiki for a proper explanation.  As an avid scripter, the Bash Guide at Greg's Wiki was the single best resource I ever read.

Last edited by alphaniner (2012-03-05 20:28:42)


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#4 2012-03-05 20:29:48

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Replacing text between backslashes

Or, in bash with pe:

 echo "${gg[1]##*/}"
.bashrc

# waaay too slow...

Last edited by jasonwryan (2012-03-05 20:30:27)


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2012-03-05 22:10:15

Prowler
Member
Registered: 2011-05-16
Posts: 16

Re: Replacing text between backslashes

skanky wrote:
$basename /home/discharge/.bashrc
.bashrc
 $sed 's,/.*/,,' <<< '/home/discharge/.bashrc'
.bashrc

smile

Edit, alphaniner's solution is probably best though. I keep forgetting about pe. neutral

I didn't even suspect there was a bash script lying around just for that purpose! Of the three that is the simplest to use. And thanks for the sed one, it was chopping my mind how i couldnt get it working - started building from an old sed syntax i used -

sed 's/oldtext/newtext/g' file > newfile

Looks like using commas instead of backslashes is better, i couldnt get ur example working with backslashes.
Btw i guess on these two u missed the brackets

$(basename /home/discharge/.bashrc)  $(sed 's,/.*/,,' <<< '/home/discharge/.bashrc') 

@alphaniner - I suppose yours is the best to use, because it has much more options after learning this Parameter expansion topic.

In the end, tried all of them, my script seems to work both ways for bkup and restoring from one, thanks for the tips, would have spent way more time on that if it weren't for you guys wink

Last edited by Prowler (2012-03-05 22:17:57)

Offline

#6 2012-03-05 23:16:21

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Replacing text between backslashes

With Gnu sed you can use most characters instead of the / as a delimiter - check the man page for those you can't use. I picked , at random really.
You don't need the brackets.

I think the main reason why the parameter expansion is best is that it's a bash built-in rather than a separate app (and process), though it is also more flexible generally, with all the options. It isn't portable to (all) other shells though, though with Arch that's probably  not an issue. Also the <<< is a bashism too, but sed could be fed via a pipe, so it's possibly the most portable of the lot - I don't know how ubiquitous basename is.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#7 2012-03-06 02:02:23

Bellum
Member
Registered: 2011-08-24
Posts: 230

Re: Replacing text between backslashes

I use fex for this.

Example:

echo $PWD | fex /{-1}

http://aur.archlinux.org/packages.php?ID=56711

Last edited by Bellum (2012-03-06 02:04:14)

Offline

#8 2012-03-06 07:08:41

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: Replacing text between backslashes

As pointed out above bash has it already built in; there's nothing quicker than using that bash functionality unless you're not using bash as your shell - and 90% of the people in this thread will probably fit that description.


Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

#9 2012-03-06 07:47:54

stqn
Member
Registered: 2010-03-19
Posts: 1,191
Website

Re: Replacing text between backslashes

/ is called a slash, \ is a backslash.

Offline

#10 2012-03-06 15:32:19

Bellum
Member
Registered: 2011-08-24
Posts: 230

Re: Replacing text between backslashes

.:B:. wrote:

As pointed out above bash has it already built in; there's nothing quicker than using that bash functionality unless you're not using bash as your shell - and 90% of the people in this thread will probably fit that description.

I use dash for scripts when I can, actually.

Offline

Board footer

Powered by FluxBB