LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 02-24-2007, 10:59 AM   #1
LocoMojo
Member
 
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165

Rep: Reputation: 30
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
 
Old 02-24-2007, 11:04 AM   #2
LocoMojo
Member
 
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165

Original Poster
Rep: Reputation: 30
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
 
Old 02-26-2007, 04:52 AM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 640

Rep: Reputation: 375Reputation: 375Reputation: 375Reputation: 375
Hello!

Take a look at `man basename'.
 
Old 02-26-2007, 05:10 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,312

Rep: Reputation: 175Reputation: 175
you should ask what you need to be done, not how you think it should be done
 
Old 02-26-2007, 07:37 AM   #5
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Code:
echo "/home/me/documents/resume" | sed 's/^.*\///g'
 
Old 02-26-2007, 08:48 AM   #6
LocoMojo
Member
 
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by firstfire
Hello!

Take a look at `man basename'.
Hi Firstfire,

Thanks for that, I'll look into basename.

LocoMojo
 
Old 02-26-2007, 08:54 AM   #7
LocoMojo
Member
 
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165

Original Poster
Rep: Reputation: 30
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
 
Old 02-26-2007, 08:58 AM   #8
LocoMojo
Member
 
Registered: Oct 2004
Distribution: Slackware 12
Posts: 165

Original Poster
Rep: Reputation: 30
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
 
Old 02-26-2007, 11:15 AM   #9
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
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...
 
Old 02-26-2007, 12:30 PM   #10
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,197

Rep: Reputation: 642Reputation: 642Reputation: 642Reputation: 642Reputation: 642Reputation: 642
by and by, since idont use konqueror can someone explain if nautilus scripts are portable in konqueror ?

they are usually bash based.
 
Old 02-26-2007, 11:01 PM   #11
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
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 !
 
Old 02-27-2007, 03:25 AM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,312

Rep: Reputation: 175Reputation: 175
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 03:26 AM.
 
Old 02-27-2007, 01:16 PM   #13
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
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...
 
Old 02-27-2007, 06:01 PM   #14
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
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?
 
Old 02-28-2007, 06:51 AM   #15
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
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 06:55 AM.
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cut -d from the right instead of from the left FirmbIT Programming 2 04-20-2006 12:31 PM
CSV and CUT wwnexc Linux - Software 11 01-27-2006 12:23 AM
cut question krock923 Programming 1 10-19-2005 05:03 PM
Printing gets cut off at the left margin kkempter Linux - General 1 03-31-2005 03:03 PM
Ok, maybe I'm not cut out for linux... goosegg Linux - Newbie 5 09-01-2003 04:43 PM


All times are GMT -5. The time now is 07:07 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration