The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
extracting date from a filename laiko UNIX for Dummies Questions & Answers 5 05-09-2008 02:15 AM
mv Filename variable to another filename gzs553 Shell Programming and Scripting 3 04-14-2008 01:19 PM
shortcut for tar cvf - [filename] | gzip > [filename].tar.gz bcamp1973 UNIX for Dummies Questions & Answers 4 12-11-2007 02:45 PM
Extracting Filename from Fullpath njoshi UNIX for Dummies Questions & Answers 3 04-19-2007 02:34 PM
extracting from tar.bz2 Raom UNIX for Advanced & Expert Users 1 03-07-2006 07:33 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 08-24-2005
Registered User
 

Join Date: Aug 2005
Posts: 3
extracting only the filename without its extenstion

Hi,

I have a requirement that i need to store only the filename without its extension. Can anyone please help me to do this.

For Example, i have stored the filename in a varialble called fname. I need to extract all the charecters before the first occurence of the dot.
If fname has value as PG_0008_20050519.xml.gz then i need to extract only "PG_0008_20050519". Please help me to do this.

TIA.
Reply With Quote
Forum Sponsor
  #2  
Old 08-24-2005
Registered User
 

Join Date: Dec 2000
Location: Greater Dallas area
Posts: 405
If the extension is always the same:
Code:
fname=`basename $fname xml.gz`
If the extension is not always the same:
Code:
fname=`echo $fname | cut -d\. -f1`
fname=${fname}.
__________________
[url=http://chuckb.1le.net/]My website[/url]
Reply With Quote
  #3  
Old 08-24-2005
Registered User
 

Join Date: Aug 2005
Posts: 3
Works Perfect.

Thanks a lot for the help.
Reply With Quote
  #4  
Old 08-25-2005
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,609
some more ways

the first is one with awk
echo `basename <program-path>` | awk -F"." '{print $1}'

this one is with sed but no of characters in extension need to be known in prior
for 3 character extension
echo `basename <program-path>` | sed -e 's/\....//'

advancement to the above method, no of characters in extension need not be known

echo `basename <program-path>` | sed -e 's/\..*//'
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:32 PM.


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

Content Relevant URLs by vBSEO 3.2.0