Linux - General This forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
08-21-2008, 08:19 AM
#1
LQ Newbie
Registered: Aug 2008
Posts: 6
Thanked: 0
How to get file extension (without the dot)
[
Log in to
get rid of this advertisement]
Hello,
I would like to know what is the extension of a file
For example
filename="file.txt"
I'd like to put "txt" in a variable called ext
I see in an other post on linuxforums org
linux-programming-scripting / 25339 / how-check-file-extensions.html
echo <filename> | grep -o '\.[^.]*$'
So I think I need to do something like
ext=`echo $filename | grep -o '\.[^.]*$'`
But I don't know how I should modify this to avoid the dot of ".txt"
and to get only "txt" in $ext.
Kind regards
08-21-2008, 08:48 AM
#2
Member
Registered: Dec 2007
Distribution: Fedora Slackware CentOS slax RHEL
Posts: 43
Thanked: 3
Quote:
Originally Posted by
scls19fr
Hello,
I would like to know what is the extension of a file
For example
filename="file.txt"
I'd like to put "txt" in a variable called ext
I see in an other post on linuxforums org
linux-programming-scripting / 25339 / how-check-file-extensions.html
echo <filename> | grep -o '\.[^.]*$'
So I think I need to do something like
ext=`echo $filename | grep -o '\.[^.]*$'`
But I don't know how I should modify this to avoid the dot of ".txt"
and to get only "txt" in $ext.
Kind regards
ext=`echo $filename |awk -F '.' '{print $2}'`
08-21-2008, 08:55 AM
#3
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,709
Thanked: 99
Code:
pwc101@test:$ filename=something.txt
pwc101@test:$ ext="${filename##*.}"
pwc101@test:$ echo $ext
txt
pwc101@test:$
See
http://tldp.org/LDP/abs/html/paramet...stitution.html for more info.
Last edited by pwc101; 08-21-2008 at 08:57 AM ..
Reason: added prompt
08-21-2008, 08:57 AM
#4
Moderator
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch+KDE
Posts: 14,165
Thanked: 382
I don't think grep allows you to search on a pattern and then only return part of the pattern.
One method--in SED--is to use a "backreference": This is a portion of a search pattern which is saved and then re-inserted later.
general form:
sed 's/part_of_pattern\(part_to_save\)more_pattern/\1/' filename
the output is simply "part_to_save", assuming that the whole pattern (between to first two "/"s) is matched.
For your example, try:
sed -n 's/.*\.\([^.]*\)$/\1/p' filename
Post back if you want it translated.....
08-21-2008, 09:01 AM
#5
Moderator
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch+KDE
Posts: 14,165
Thanked: 382
Quote:
Originally Posted by
TheMadIndian
ext=`echo $filename |awk -F '.' '{print $2}'`
Only works if the filename has just one "."
Perhaps AWK has a command for "last field"??
I ran my test on files like this:
rpmpkgs.3.gz
statistics.3.gz
Xorg.9.log
Xorg.3.log.old
08-21-2008, 09:10 AM
#6
Moderator
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch+KDE
Posts: 14,165
Thanked: 382
Quote:
Originally Posted by
pwc101
memo to self: Quit stalling and learn parameter substitution.....
Interesting counter-intuitive experiment: using filenames with several "."s, try pwc's solution with only 1 "#", and with 3 "#"s
08-21-2008, 09:19 AM
#7
Guru
Registered: Sep 2003
Location: Bologna, Italia
Distribution: OpenSUSE 11.1 CentOS 5.4 VectorLinux 6.0
Posts: 5,339
Thanked: 496
Quote:
Originally Posted by
pixellany
Perhaps AWK has a command for "last field"??
$NF is the last field. NF is the number of fields in the current record (line) so $NF means the last field. Pixellany, when will you learn awk?
08-21-2008, 09:26 AM
#8
Moderator
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch+KDE
Posts: 14,165
Thanked: 382
Quote:
Originally Posted by
colucix
Pixellany, when will you learn awk?
I'm still having too much fun solving every problem with SED---no matter how convoluted and obfuscated the code......
Or perhaps: As soon as I decide which distro is best.....
08-21-2008, 10:41 AM
#9
Member
Registered: Dec 2007
Distribution: Fedora Slackware CentOS slax RHEL
Posts: 43
Thanked: 3
Quote:
Originally Posted by
colucix
$NF is the last field. NF is the number of fields in the current record (line) so $NF means the last field. Pixellany, when will you learn awk?
beat me to it
08-21-2008, 10:53 AM
#10
Senior Member
Registered: Aug 2006
Posts: 2,161
Thanked: 156
with bash
Code:
# a=file.txt
# IFS="."
# set -- $a
# eval echo \${$#}
txt
08-21-2008, 10:57 AM
#11
Guru
Registered: Jan 2001
Posts: 23,977
Thanked: 8
Quote:
Originally Posted by
pixellany
I'm still having too much fun solving every problem with SED---no matter how convoluted and obfuscated the code......
Or perhaps: As soon as I decide which distro is best.....
awk is so.. last decade though. The real lazy admins just use the command
cut ..
08-21-2008, 12:48 PM
#12
LQ Newbie
Registered: Aug 2008
Posts: 6
Thanked: 0
Original Poster
Thanks guys
I hate regexps ;-(
I also ask the same question at
http://www.linuxforums.org/forum/lin...tml#post618136
Quote:
Originally Posted by
ghostdog74
if you are on bash
Code:
# a=file.txt
# echo ${a/*./}
txt
Last edited by scls19fr; 08-21-2008 at 12:52 PM ..
Thread Tools
Search 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
HTML code is Off
All times are GMT -5. The time now is 06:07 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Click Here to receive a complimentary subscription courtesy of LQ.
Latest Threads
LQ News
LQ Podcast
LQ Radio