LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-12-2005, 01:07 PM   #1
paraiso
Member
 
Registered: Apr 2005
Distribution: Fedora Core 4
Posts: 88

Rep: Reputation: 15
Question Command get last element


I have several path located on a file from where I would like to display only the last element (file) of those paths.

home/docs/file2
home/various/file
home/email/test.htm

The output result would be something like that:

file2
file
test.htm


How can do that?

Thanks
 
Old 05-12-2005, 01:27 PM   #2
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
You could use perl:
Code:
#!/usr/bin/perl

@lines = split(/\//,$ARGV[0]);
print $lines[$#lines]."\n";
Put that in a file called trunc or something, make it executable (chmod +x trunc) and put it somewhere in your path. Then:
trunc /path/to/file
will print out:
file
 
Old 05-12-2005, 02:49 PM   #3
paraiso
Member
 
Registered: Apr 2005
Distribution: Fedora Core 4
Posts: 88

Original Poster
Rep: Reputation: 15
Thank you Komakino for the perl script ! Is that the only way ? I needed this truncation to be one of the step actions within a shell script.

Cheers
 
Old 05-12-2005, 05:40 PM   #4
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
Quote:
Originally posted by paraiso
Thank you Komakino for the perl script ! Is that the only way ? I needed this truncation to be one of the step actions within a shell script.

Cheers
Hmm, well the same thing must be possible in bash...

But assuming you have perl installed, you can still call it as part of a bash script. Something like:
Code:
i=`perl trunc /usr/bin/file`
echo $i
will echo the truncated filename.
Sorry I can't be more help, I do most of my scripting in Perl.
 
Old 05-12-2005, 06:19 PM   #5
paraiso
Member
 
Registered: Apr 2005
Distribution: Fedora Core 4
Posts: 88

Original Poster
Rep: Reputation: 15
Thanks
 
  


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
index of an element in the array ? thelonius Programming 1 09-24-2005 12:41 PM
number of element in a array of pointer? os2 Programming 7 03-09-2005 11:50 AM
Retrieving element content in libxml++-1.0 SeriousMan Programming 4 11-22-2004 12:14 PM
C++ Six Element Array petercool Programming 2 08-20-2003 11:08 AM
heaviest element Aussie General 9 05-13-2002 09:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:40 AM.

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