Solved

Get Extension

Posted on 2004-08-11
9
165 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
 

Author Comment

by:ansi_c
ID: 11781904
i am doing unix shell scripting.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now