LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Command get last element (https://www.linuxquestions.org/questions/linux-newbie-8/command-get-last-element-322806/)

paraiso 05-12-2005 01:07 PM

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

Komakino 05-12-2005 01:27 PM

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

paraiso 05-12-2005 02:49 PM

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

Komakino 05-12-2005 05:40 PM

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.

paraiso 05-12-2005 06:19 PM

Thanks :)


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