The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Stripping out extension in file name devs Shell Programming and Scripting 9 05-14-2008 01:53 AM
Truncate multiple file extensions prvnrk Shell Programming and Scripting 12 04-04-2008 07:20 AM
Stripping out the extension of a file name ramky79 Shell Programming and Scripting 2 12-27-2006 10:25 AM
find -regex: matching multiple extensions r0sc0 Shell Programming and Scripting 2 12-08-2005 10:32 AM
stripping last lien off a file vivekshankar UNIX for Dummies Questions & Answers 3 05-31-2005 03:35 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: May 2008
Posts: 3
Stripping out extensions when file has multiple dots in name

I posted this already in another thread, but was told that I should create a seperate thread for the following question:

How do I strip the extension when the delimiter might occur multiple times in the filename?

For example:
I have 2 files as input for my script.
test.extension
test.foo.extension

In my script I want to see "test" and "test.foo" as results.
But the following script-snippet gives "test" for both files.
I know this is caused by the -f1 that I use as setting for cut. But I want to know which command I should use so it starts looking for the delim-character from the right i.s.o the left (probably a different command than cut, but which?)
Code:
!/bin/sh

FILE_NAME=$1
if [ ! -r ${FILE_NAME} ]
then
  echo "Could not find file ${FILE_NAME}"
  exit 1
fi

FNAME=`echo "${FILE_NAME}"| cut -f1 -d'.'`

echo "FNAME = ${FNAME}"

TIA
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-14-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,923
Try basename
Reply With Quote
  #3 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: Apr 2008
Location: Bangalore
Posts: 120
Hi

Instead of FNAME=`echo "${FILE_NAME}"| cut -f1 -d'.'` , try the below line



FNAME=`echo "${FILE_NAME}"| sed 's#^\(.*\)\.\(.*\)#\1#' `

Thanks
Penchal
Reply With Quote
  #4 (permalink)  
Old 05-14-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,615
Try this:

Code:
echo 'test.foo.extension' | sed 's/\(.*\)\..*/\1/'
Regards
Reply With Quote
  #5 (permalink)  
Old 05-14-2008
Registered User
 

Join Date: May 2008
Posts: 3
Quote:
Originally Posted by Franklin52 View Post
Try this:

Code:
echo 'test.foo.extension' | sed 's/\(.*\)\..*/\1/'
Regards
Thanx.

After checking with a coleague who has used sed before in the past I understand what the regular expresion means:

"replace s with any number of characters followed by a dot followed by any number of characters and store the first set of any number of characters".

What he could not explain to me is why it finds the last dot in test.foo.extension and not the first dot (thus why it actually works). Since both "any number of characters" may contain dots (in theory) if I understand the man-pages (and his explanation) correctly.

Anyway since ".extension" is a fixed extension he came up with another solution (after reading your solution), which also works when the extension is not there (by accident) :
Code:
FNAME=echo "${FILE_NAME}"|sed 's/\(.*\)\.program/\1/'
(The actual "extension" is ".program")

Last edited by Nemelis; 05-14-2008 at 04:10 AM. Reason: small typo
Reply With Quote
  #6 (permalink)  
Old 05-14-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,923
The asterisk will prefer "longest leftmost" so that decides on which side any "extra" dots will go.

If the extension name is always the same, I'll say "basename" again.
Reply With Quote
  #7 (permalink)  
Old 05-14-2008
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 436
Quote:
Originally Posted by Nemelis View Post
Code:
FNAME=`echo "${FILE_NAME}"| cut -f1 -d'.'`
echo can do the job, anything else is useless.
Code:
FNAME=`echo "${FILE_NAME%.*}"`
Regards,
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:40 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0