LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux 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
 
Search this Thread
Old 08-21-2008, 08:19 AM   #1
scls19fr
LQ Newbie
 
Registered: Aug 2008
Posts: 6

Rep: Reputation: 0
How to get file extension (without the dot)


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
 
Old 08-21-2008, 08:48 AM   #2
TheMadIndian
Member
 
Registered: Dec 2007
Distribution: Fedora Slackware CentOS slax RHEL
Posts: 114

Rep: Reputation: 23
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}'`
 
Old 08-21-2008, 08:55 AM   #3
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
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
 
Old 08-21-2008, 08:57 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
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.....
 
Old 08-21-2008, 09:01 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
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
 
Old 08-21-2008, 09:10 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
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
 
Old 08-21-2008, 09:19 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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?
 
Old 08-21-2008, 09:26 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
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.....
 
Old 08-21-2008, 10:41 AM   #9
TheMadIndian
Member
 
Registered: Dec 2007
Distribution: Fedora Slackware CentOS slax RHEL
Posts: 114

Rep: Reputation: 23
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
 
Old 08-21-2008, 10:53 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
with bash
Code:
# a=file.txt
# IFS="."
# set -- $a
# eval echo \${$#}
txt
 
Old 08-21-2008, 10:57 AM   #11
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 199Reputation: 199
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..
 
Old 08-21-2008, 12:48 PM   #12
scls19fr
LQ Newbie
 
Registered: Aug 2008
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
  


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 On
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 03:48 AM.

Main Menu
Advertisement
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.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration