LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 07: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, 07:48 AM   #2
TheMadIndian
Member
 
Registered: Dec 2007
Distribution: Fedora Slackware CentOS slax RHEL
Posts: 117

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, 07: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 07:57 AM. Reason: added prompt
 
Old 08-21-2008, 07:57 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 08:01 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 08:10 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 08:19 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 08:26 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 09:41 AM   #9
TheMadIndian
Member
 
Registered: Dec 2007
Distribution: Fedora Slackware CentOS slax RHEL
Posts: 117

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, 09:53 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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

Rep: Reputation: 269Reputation: 269Reputation: 269
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, 11:48 AM   #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 11:52 AM.
 
  


Reply



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 04:19 PM
[C] writing floating point to file- coma instead of dot slomek Programming 4 04-12-2007 03:23 PM
dot space dot binary_0011 Linux - Newbie 5 09-14-2006 07:43 AM
why when redirecting output to a file any file extension seems to be fine? dr_zayus69 Linux - General 1 05-21-2005 04: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 07:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:09 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
Open Source Consulting | Domain Registration