Solved

strip suffix from full path filename

Posted on 1998-06-16
5
1,064 Views
Last Modified: 2012-06-27
hello,
as far as I know command  basename can strip directory and suffix from filename.
But I want to strip only suffix from filename but keep leading directory components. Is it possible? How?
Thanks
0
Comment
Question by:tian
5 Comments
 
LVL 2

Accepted Solution

by:
blowfish earned 100 total points
Comment Utility
basename strips off the full path leaving only the filename.

dirname strips off the filename leaving only the path
0
 
LVL 2

Expert Comment

by:bertvermeerbergen
Comment Utility
If you are using ksh, you can use the following feature:
You can strip any suffix sequence from a variable called myname with the syntax like ${myname#abc} where abc is the suffix you want to get rid of.  You can use wildcards here, like '.*' to strip any trailing sequence after (and including) the dot.
So:
  myname=/a/b/c/file.ext
  echo ${myname#.*}
will print out the string "/a/b/c/file".
The feature is more general, you can use ## to match and strip the largest match, or use % or %% to strip prefix sequences.
0
 

Author Comment

by:tian
Comment Utility
dirname is just what I'm looking for.
I have tried  
         #!/usr/bin/ksh  
         myname=/a/b/c/file.ext
         echo ${myname#.*}
but it does not work.
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
assuming you're using any kind of shell with myname=/aaa.b/b/c/file.d.ext :

csh#   echo $myname:r
sh#    csh -f -c "set m=$myname; echo \$m:r"
sh#    echo $myname|sed  -e  's/\(.*\)\..*$/\1/'
sh#    echo $myname|gawk -F. '{for (i=1;i<NF-1;i++) {printf("%s.",$i);} print $i}'
ksh#   echo ${myname%.*}

0
 
LVL 2

Expert Comment

by:bertvermeerbergen
Comment Utility
Sorry, I seem to have reversed the % and # signification  (Even worse, I seem to be doing this all the time).  Still, remember the syntax exists because it can be very useful in shell programming.
FYI: bash (popular on Linux systems) also supports this syntax.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now