Solved

strip suffix from full path filename

Posted on 1998-06-16
5
1,082 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
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 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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

617 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