You are not logged in.
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 ![]()
Offline
$basename /home/discharge/.bashrc
.bashrc $sed 's,/.*/,,' <<< '/home/discharge/.bashrc'
.bashrc![]()
Edit, alphaniner's solution is probably best though. I keep forgetting about pe. ![]()
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
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
Or, in bash with pe:
echo "${gg[1]##*/}"
.bashrc# waaay too slow...
Last edited by jasonwryan (2012-03-05 20:30:27)
Offline
$basename /home/discharge/.bashrc .bashrc$sed 's,/.*/,,' <<< '/home/discharge/.bashrc' .bashrc
Edit, alphaniner's solution is probably best though. I keep forgetting about pe.
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 > newfileLooks 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 ![]()
Last edited by Prowler (2012-03-05 22:17:57)
Offline
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
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
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
/ is called a slash, \ is a backslash.
Offline
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