Solved

Get Extension

Posted on 2004-08-11
9
170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 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