Solved

strip suffix from full path filename

Posted on 1998-06-16
5
1,077 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 2

Accepted Solution

by:
blowfish earned 100 total points
ID: 2008083
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
ID: 2008084
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
ID: 2008085
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
ID: 2008086
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
ID: 2008087
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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…
Suggested Courses

752 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