Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
02-24-2007, 09:59 AM
|
#1
|
|
Member
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165
Rep:
|
Cut from right to left?
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
|
|
|
|
02-24-2007, 10:04 AM
|
#2
|
|
Member
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165
Original Poster
Rep:
|
Quote:
|
Originally Posted by LocoMojo
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.
LocoMojo
|
|
|
|
02-26-2007, 03:52 AM
|
#3
|
|
Member
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 577
|
Hello!
Take a look at `man basename'.
|
|
|
|
02-26-2007, 04:10 AM
|
#4
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
you should ask what you need to be done, not how you think it should be done 
|
|
|
|
02-26-2007, 06:37 AM
|
#5
|
|
Member
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383
Rep:
|
Code:
echo "/home/me/documents/resume" | sed 's/^.*\///g'
|
|
|
|
02-26-2007, 07:48 AM
|
#6
|
|
Member
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165
Original Poster
Rep:
|
Quote:
|
Originally Posted by firstfire
Hello!
Take a look at `man basename'.
|
Hi Firstfire,
Thanks for that, I'll look into basename.
LocoMojo
|
|
|
|
02-26-2007, 07:54 AM
|
#7
|
|
Member
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165
Original Poster
Rep:
|
Quote:
|
Originally Posted by bigearsbilly
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.
Thanks,
LocoMojo
|
|
|
|
02-26-2007, 07:58 AM
|
#8
|
|
Member
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165
Original Poster
Rep:
|
Quote:
|
Originally Posted by kshkid
Code:
echo "/home/me/documents/resume" | sed 's/^.*\///g'
|
Hi Kshkid,
That seems to be far more elegant than what I wound up doing:
Code:
echo "/home/me/documents/resume" | rev | cut -d "/" -f 2- | rev
I really need to break down and learn about sed and awk.
Thanks,
LocoMojo
|
|
|
|
02-26-2007, 10:15 AM
|
#9
|
|
Member
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 498
Rep:
|
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...
|
|
|
|
02-26-2007, 11:30 AM
|
#10
|
|
Senior Member
Registered: May 2005
Location: boston, usa
Distribution: fc-12/ fc-11-live-usb/ aix
Posts: 2,733
|
by and by, since idont use konqueror can someone explain if nautilus scripts are portable in konqueror ?
they are usually bash based.
|
|
|
|
02-26-2007, 10:01 PM
|
#11
|
|
Member
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383
Rep:
|
Quote:
|
Originally Posted by alienDog
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 ! 
|
|
|
|
02-27-2007, 02:25 AM
|
#12
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
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.
|
|
|
|
02-27-2007, 12:16 PM
|
#13
|
|
Member
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 498
Rep:
|
Quote:
|
Originally Posted by kshkid
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...
|
|
|
|
02-27-2007, 05:01 PM
|
#14
|
|
Member
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mandriva, Ubuntu, LFS, gNewSense
Posts: 221
Rep:
|
Quote:
|
Originally Posted by LocoMojo
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?
|
|
|
|
02-28-2007, 05:51 AM
|
#15
|
|
Member
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 498
Rep:
|
Quote:
|
Originally Posted by kshkid
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.
Last edited by alienDog; 02-28-2007 at 05:55 AM.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:40 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|