UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!
  #1  
Old May 28th, 2003, 08:33 AM
faubertr faubertr is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Stamford, CT
Posts: 5 faubertr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unix Script.

Hi,
A full path is coming as my 1 argument. I would like to know how I can only read the file extension from this argument.

Thanks for the help.

Robert,

Reply With Quote
  #2  
Old May 28th, 2003, 02:32 PM
faubertr faubertr is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Stamford, CT
Posts: 5 faubertr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I have already found my answer which may help others to find this extension.

extension=$(echo $1 | awk -F. '{print $2}')

Where $1 is the full path including the filename & extension.

Reply With Quote
  #3  
Old June 3rd, 2003, 11:20 AM
faubertr faubertr is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Stamford, CT
Posts: 5 faubertr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Lightbulb Unix -Hp Directory.

Thanks for the help and the answer is :

NAME=$(dirname /home/faubertr/reports/text.ps)

faubertr:/home/faubertr>echo $NAME
/home/faubertr/reports


not exactly what's showing in examples from the man page.

Reply With Quote
  #4  
Old August 5th, 2003, 09:07 PM
zedmelon's Avatar
zedmelon zedmelon is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: under a rock
Posts: 54 zedmelon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 20 m 42 sec
Reputation Power: 5
awk is probably not best for this...

faubertr...

The awk thing will work, *BUT* it assumes that there are no dots in the middle of the filename, such as

/usr/local/storage/docs/old.resume.doc

If you try the suggestion below, it will give you the extension of the file every time, plus, it's much shorter than piping the whole thing through awk. Note that if there is no extension, it will return nothing, or for the above file without extension, you'd get "resume" as a result.

(hope you don't mind the korn shell... I write all my scripts in it)

Even though this font makes percent signs look like "96," I can show you how to glean an extension from a full path and give you some related information that's made my own scripting MUCH cleaner.

#!/bin/ksh
completevar=$1
filename=${1##*/}
extension=${1##*.} <--the one you're looking for now
fullpath=${1%/*}
nothing=${1%%/*}

Using the variable "/usr/local/storage/docs/old.resume.doc" in each of the above lines would give you these results:
completevar: /usr/local/storage/docs/old.resume.doc
filename: old.resume.doc
extension: doc
fullpath: /usr/local/storage/docs
nothing: (nothing, since the entire variable matches the string)

1. The dollar and braces around the variable name tell the shell to use that variable as a base.
2. The string ("*." for your file extension) is what you want removed. Matching to "*." will find a dot and any or all characters to the left of it.
3. The pound signs tell the shell to strip the largest portion of the variable that matches the string. If you use just ONE pound sign, it'll strip the *smallest* match.
4. Use percent signs to strip from the end of the variable, use pound signs to strip from the beginning.

Hope this was helpful, instead of "too much crap with an answer mixed into it."
;)

Enjoy!
-zedmelon

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Unix Script.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway