ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Is there a way I could use the cut command going from right to left? For example I want to cut from the last "/" in a url:
/home/me/documents/resume
I want "resume" in the output.
In this instance I know I could use:
cut -d "/" -f 4
But that would only work for directories 4 deep. I want this to work for directories 2, 3, 5, 6 deep.
Is there a way I could cut the first "/" going from right to left?
Another question:
Using a bash script, how do I grab Konqueror's "$PWD". In other words, if I navigate to /home/me/documents/resume in konqueror, how can I grab that url in bash? I'm not very familiar with DCOP, is there a way I could get it via DCOP or something?
Is there a way I could use the cut command going from right to left? For example I want to cut from the last "/" in a url:
/home/me/documents/resume
I want "resume" in the output.
In this instance I know I could use:
cut -d "/" -f 4
But that would only work for directories 4 deep. I want this to work for directories 2, 3, 5, 6 deep.
Is there a way I could cut the first "/" going from right to left?
Another question:
Using a bash script, how do I grab Konqueror's "$PWD". In other words, if I navigate to /home/me/documents/resume in konqueror, how can I grab that url in bash? I'm not very familiar with DCOP, is there a way I could get it via DCOP or something?
Thanks in advance.
LocoMojo
Ooops, I got it.
I wasn't aware of the "rev" command.
Still looking for the Konqueror's url deal though.
you should ask what you need to be done, not how you think it should be done
Hello Bigbearsbilly,
I need for a bash script to get the current working directory of Konqueror.
In other words, when I call a bash script from within Konqueror via a service menu I need for the bash script to know which directory Konqueror is currently in.
Sed isn't too bad, but my humble opinion is that rev|cut|rev is more elegant. It might also be faster to execute than sed (not sure though, would need testing). On a single "cut" the difference might be marginal, but if you do massive amounts of cutting it may make a big difference.
Sed isn't too bad, but my humble opinion is that rev|cut|rev is more elegant. It might also be faster to execute than sed (not sure though, would need testing). On a single "cut" the difference might be marginal, but if you do massive amounts of cutting it may make a big difference.
I guess it's also a matter of taste...
But with the other option, there is a need to use too many processes and kernel data structures where the task could be done with just 1 process and also without any kernel data structures ( assuming input to be in a file and piped through the output of other process )
So, I think the solution with sed should be economical and faster !
why you need to know what dir konqueror is in?
don't you just need to know the cwd of the script.
i should think konq will just be in the directory it lives in.
$PPID is the parent of the shell script.
do ptree $PPID see what happens
oops, ptree is solaris, do you have on linux?
Last edited by bigearsbilly; 02-27-2007 at 02:26 AM.
But with the other option, there is a need to use too many processes and kernel data structures where the task could be done with just 1 process and also without any kernel data structures ( assuming input to be in a file and piped through the output of other process )
So, I think the solution with sed should be economical and faster !
Could, could be... I was just guessing Sed usually isn't blazingly fast...
Is there a way I could use the cut command going from right to left? For example I want to cut from the last "/" in a url:
/home/me/documents/resume
I want "resume" in the output.
In this instance I know I could use:
cut -d "/" -f 4
But that would only work for directories 4 deep. I want this to work for directories 2, 3, 5, 6 deep.
Is there a way I could cut the first "/" going from right to left?
var=/home/me/documents/resume
basename=${var##*/}
Quote:
Another question:
Using a bash script, how do I grab Konqueror's "$PWD". In other words, if I navigate to /home/me/documents/resume in konqueror, how can I grab that url in bash? I'm not very familiar with DCOP, is there a way I could get it via DCOP or something?
Where do you want to "grab" it? What do you want to do with it?
But with the other option, there is a need to use too many processes and kernel data structures where the task could be done with just 1 process and also without any kernel data structures ( assuming input to be in a file and piped through the output of other process )
So, I think the solution with sed should be economical and faster !
Well... curiosity got the better of me and I just had to test it Actually rev|cut|rev seems to be significantly faster than cat|sed, here's how I tested it:
Code:
#!/bin/bash
# Make testfile with 500000 entries if it doesn't exist already:
if [ ! -e "testfile" ]; then
START=$(date +%s)
for i in $(seq 500000); do
echo "/foo/bar/bax" >> testfile
done
echo "Creating a testfile took $(expr $(date +%s) - $START) seconds"
fi
# With rev|cut|rev:
START=$(date +%s)
rev testfile | cut -f 1 -d "/" | rev > /dev/null
echo "rev|cut|rev took: $(expr $(date +%s) - $START) seconds"
# With cat|sed:
START=$(date +%s)
cat testfile | sed 's/^.*\///g' > /dev/null
echo "cat|sed took: $(expr $(date +%s) - $START) seconds"
With rev|cut|rev I get an average of 3 seconds and with cat|sed >10 seconds... My machine is real old, though, and with newer machines the differences might be much smaller, if even existant.
Basename works fine as long as you're dealing with files and their paths, but it doesn't work with other types of data.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.