Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

strip suffix from full path filename

Posted on 1998-06-16
5
Medium Priority
?
1,085 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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
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…
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…
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.

704 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