Solved

Get Extension

Posted on 2004-08-11
9
168 Views
Last Modified: 2010-04-22
Hey.. Suppose I have a variable that stores the below value
    "abc/def/ghi.txt"

I wish to get the extension which in this case would be txt. How to do that? Note that the length of the extension varies.
Full working example preferred.
0
Comment
Question by:ansi_c
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 11780831
Hi

What language are you using?

Regards,
Zyloch
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11780834
Just to let you know, most often this is done with some function like split() in the language, but we have to be sure to give a working example.

Regards
0
 
LVL 8

Expert Comment

by:da99rmd
ID: 11780998
Hi ansi_c,
yes it would be easier if we know what language :)

in php you do like this:

$arr = explode(".",$variable);
$extention = arr[count[arr]];

in bash do this:

ext=`echo $variable | sed 's/.*\([^\.]\+\)\.\([^\.]\+\)$/\2/'`

in c:

bool getextention(char *in, char *out)
{
    char *tmp = in;
    char *punkt;
   
    for(int i=0; i < sizeof(*in);i++;)
        {
            if(tmp++ == '.')
                punkt = tmp;
        }
    if(tmp != in)
        {
            out = tmp;
            return TRUE;
        }
    return FALSE;
}


/Rob
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:ansi_c
ID: 11781904
i am doing unix shell scripting.
0
 
LVL 8

Assisted Solution

by:da99rmd
da99rmd earned 25 total points
ID: 11782224
ansi_c,
then this will apply:
ext=`echo $variable | sed 's/.*\([^\.]\+\)\.\([^\.]\+\)$/\2/'`
/Rob
0
 
LVL 6

Accepted Solution

by:
de2Zotjes earned 25 total points
ID: 11787595
If you are using bash and protability is not an issue you can use:

${variable##*.}
0
 
LVL 8

Expert Comment

by:da99rmd
ID: 11790820
de2Zotjes,
smothe solution there.
/Rob
0
 
LVL 6

Expert Comment

by:de2Zotjes
ID: 11797007
smothe ?

I am not native english speaking, what am I missing?
0
 
LVL 8

Expert Comment

by:da99rmd
ID: 11827942
de2Zotjes,
sry I cant spell it should be "smooth" and it means nice, easy in this purpose otherwise it can mean flat, even.
However it was a compliment :)
/Rob
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question