|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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, ![]() |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Unix Script. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|