LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - General
User Name
Password
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

Reply
 
Thread Tools Search this Thread
Old 08-21-2008, 08:19 AM   #1
scls19fr
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
scls19fr is offline     Reply With Quote
Old 08-21-2008, 08:48 AM   #2
TheMadIndian
Member
 
Registered: Dec 2007
Distribution: Fedora Slackware CentOS slax RHEL
Posts: 43
Thanked: 3
Quote:
Originally Posted by scls19fr View Post
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}'`
TheMadIndian is offline     Reply With Quote
Old 08-21-2008, 08:55 AM   #3
pwc101
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
pwc101 is offline     Reply With Quote
Old 08-21-2008, 08:57 AM   #4
pixellany
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.....
pixellany is offline     Reply With Quote
Old 08-21-2008, 09:01 AM   #5
pixellany
Moderator
 
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch+KDE
Posts: 14,165
Thanked: 382
Quote:
Originally Posted by TheMadIndian View Post
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
pixellany is offline     Reply With Quote
Old 08-21-2008, 09:10 AM   #6
pixellany
Moderator
 
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch+KDE
Posts: 14,165
Thanked: 382
Quote:
Originally Posted by pwc101 View Post
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.
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
pixellany is offline     Reply With Quote
Old 08-21-2008, 09:19 AM   #7
colucix
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 View Post
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?
colucix is online now     Reply With Quote
Old 08-21-2008, 09:26 AM   #8
pixellany
Moderator
 
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch+KDE
Posts: 14,165
Thanked: 382
Quote:
Originally Posted by colucix View Post
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.....
pixellany is offline     Reply With Quote
Old 08-21-2008, 10:41 AM   #9
TheMadIndian
Member
 
Registered: Dec 2007
Distribution: Fedora Slackware CentOS slax RHEL
Posts: 43
Thanked: 3
Quote:
Originally Posted by colucix View Post
$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
TheMadIndian is offline     Reply With Quote
Old 08-21-2008, 10:53 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,161
Blog Entries: 5
Thanked: 156
with bash
Code:
# a=file.txt
# IFS="."
# set -- $a
# eval echo \${$#}
txt
ghostdog74 is offline     Reply With Quote
Old 08-21-2008, 10:57 AM   #11
trickykid
Guru
 
Registered: Jan 2001
Posts: 23,977
Thanked: 8
Quote:
Originally Posted by pixellany View Post
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..
trickykid is offline     Reply With Quote
Old 08-21-2008, 12:48 PM   #12
scls19fr
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 View Post
if you are on bash
Code:
# a=file.txt
# echo ${a/*./}
txt

Last edited by scls19fr; 08-21-2008 at 12:52 PM..
scls19fr is offline     Reply With Quote

Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux file extension vs Dos file Extension? manaa Linux - Newbie 6 02-12-2009 05:19 PM
[C] writing floating point to file- coma instead of dot slomek Programming 4 04-12-2007 04:23 PM
dot space dot binary_0011 Linux - Newbie 5 09-14-2006 08:43 AM
why when redirecting output to a file any file extension seems to be fine? dr_zayus69 Linux - General 1 05-21-2005 05:09 AM
useradd not accepting dot so i am not able to use dot in mail id hanu_raob Linux - General 5 05-13-2004 08:04 AM


All times are GMT -5. The time now is 06:07 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Advertisement
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.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration