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 extensions when file has multiple dots in name Nemelis Shell Programming and Scripting 8 05-14-2008 05:12 AM
How to get file extension shirleyeow Shell Programming and Scripting 17 01-17-2008 04:40 AM
Stripping out the extension of a file name ramky79 Shell Programming and Scripting 4 12-28-2006 06:15 PM
Stripping out the extension of a file name ramky79 Shell Programming and Scripting 2 12-27-2006 10:25 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 02-24-2007
Registered User
 

Join Date: Feb 2007
Posts: 12
Stripping out extension in file name

This command gives me just the filename without any extension:
evrvar =`echo filename.tar | sed 's/\.[^.]*$//'`

I am trying to make a change to this command... to make it work for... filename.tar.gz to get just the filename....

currently the command gives me filename.tar by removing only gz... I am new to regular expression... pls help.

Thx.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-24-2007
Registered User
 

Join Date: Aug 2005
Location: Bangalore
Posts: 196
try this:
Code:
echo filename.tar.gz |awk -F. '{ print $1 }'
or
Code:
echo filename.tar.gz |cut -d'.' -f1
or sed solution
Code:
echo "filename.tar.gz" | sed 's/\(.*\)\.\(.*\)\.\(.*\)/\1/g'
or
Code:
echo "filename.tar.gz" | sed 's/.......$//g'

Last edited by ahmedwaseem2000; 02-25-2007 at 01:08 AM.
Reply With Quote
  #3 (permalink)  
Old 02-25-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,391
If filename does not contain period(s):

Code:
$ fname="filename.tar.gz"
$ echo "${fname%%.*}"
filename

Otherwise:

Code:
${fname%.*.*}

Last edited by radoulov; 02-25-2007 at 04:15 AM.
Reply With Quote
  #4 (permalink)  
Old 02-25-2007
Registered User
 

Join Date: Feb 2007
Posts: 12
Thanks for the replies.

can this regular expression be made possible to use for both filename.gz & filename.tar.gz

I want to get filename for both of these commands as just filename only:
echo "filename.tar.gz" | sed 's/\(.*\)\.\(.*\)\.\(.*\)/\1/g'
echo "filename.gz" | sed 's/\(.*\)\.\(.*\)\.\(.*\)/\1/g'

thanx in advance.
Reply With Quote
  #5 (permalink)  
Old 02-25-2007
Registered User
 

Join Date: Aug 2005
Location: Bangalore
Posts: 196
You could use either the awk or the cut solutions they will work in all the scenarios. Here is the sed solution to generalize on both the occasions.

Code:
 echo "filename.tar" | sed "s/\([^.*]\)\.\(.*\)*$/\1/g"

Last edited by ahmedwaseem2000; 02-25-2007 at 08:09 AM.
Reply With Quote
  #6 (permalink)  
Old 02-25-2007
Registered User
 

Join Date: Jan 2007
Posts: 364
Quote:
Originally Posted by ahmedwaseem2000
You could use either the awk or the cut solutions they will work in all the scenarios. Here is the sed solution to generalize on both the occasions.

Code:
 echo "filename.tar" | sed "s/\([^.*]\)\.\(.*\)*$/\1/g"
echo "filename.tar" | sed "s/\..*$//"
Reply With Quote
  #7 (permalink)  
Old 02-26-2007
Registered User
 

Join Date: Feb 2007
Posts: 12
thank you all.. it was helpful
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:58 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