Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

cut filename extension

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-28-2010
Essbaumer Essbaumer is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 28 July 2010, 3:31 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
cut filename extension

I need a small script (sh) to remove in a variable the filename extension.
Example:
f = "testfile.txt"
and I need a $a with "testfile".
Some one a idea?
Sponsored Links
    #2  
Old Unix and Linux 07-28-2010
pravin27 pravin27 is offline Forum Advisor  
Advisor
 
Join Date: Sep 2009
Last Activity: 7 September 2016, 8:47 AM EDT
Location: ./India/Bangalore
Posts: 1,271
Thanks: 70
Thanked 299 Times in 290 Posts

Code:
f=testfile.txt ; a=`basename $f ".txt"` ;echo $a

The Following User Says Thank You to pravin27 For This Useful Post:
f14f21 (07-28-2010)
Sponsored Links
    #3  
Old Unix and Linux 07-28-2010
Essbaumer Essbaumer is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 28 July 2010, 3:31 PM EDT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Linux
    #4  
Old Unix and Linux 07-28-2010
tukuyomi tukuyomi is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 27 September 2016, 1:25 AM EDT
Location: France
Posts: 320
Thanks: 19
Thanked 81 Times in 76 Posts
Using shell builtins (sh, bash and probably others too)

Code:
#!/bin/sh

a='testfile.test.txt'

echo "Filename: ${a%.*}"
echo "Extension: ${a##*.}"

exit 0


Last edited by tukuyomi; 07-28-2010 at 06:12 PM..
The Following User Says Thank You to tukuyomi For This Useful Post:
f14f21 (07-28-2010)
Sponsored Links
    #5  
Old Unix and Linux 07-29-2010
DarkSwiss DarkSwiss is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 3 September 2010, 10:06 AM EDT
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts

Code:
echo $FILENAME |sed 's/\(.*\)..../\1/'

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Filename from splitting files to have the same filename of the original file with counter value natalie23 Shell Programming and Scripting 3 11-13-2009 06:15 AM
script takes the whole filename instead of just extension wolverine999 UNIX for Dummies Questions & Answers 1 05-20-2009 03:49 PM
changing filename extension Hiso Shell Programming and Scripting 3 01-24-2009 09:02 AM
separating filename and extension lucaspewkas Shell Programming and Scripting 2 04-06-2007 08:07 AM
filename extension check - regular expression devs Shell Programming and Scripting 4 03-02-2007 03:01 PM



All times are GMT -4. The time now is 07:47 AM.