LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to get file extension (without the dot) (http://www.linuxquestions.org/questions/linux-general-1/how-to-get-file-extension-without-the-dot-664315/)

scls19fr 08-21-2008 08:19 AM

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

TheMadIndian 08-21-2008 08:48 AM

Quote:

Originally Posted by scls19fr (Post 3254784)
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}'`

pwc101 08-21-2008 08:55 AM

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.

pixellany 08-21-2008 08:57 AM

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 08-21-2008 09:01 AM

Quote:

Originally Posted by TheMadIndian (Post 3254814)
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 08-21-2008 09:10 AM

Quote:

Originally Posted by pwc101 (Post 3254823)
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

colucix 08-21-2008 09:19 AM

Quote:

Originally Posted by pixellany (Post 3254832)
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? ;)

pixellany 08-21-2008 09:26 AM

Quote:

Originally Posted by colucix (Post 3254848)
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.....

TheMadIndian 08-21-2008 10:41 AM

Quote:

Originally Posted by colucix (Post 3254848)
$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 ;)

ghostdog74 08-21-2008 10:53 AM

with bash
Code:

# a=file.txt
# IFS="."
# set -- $a
# eval echo \${$#}
txt


trickykid 08-21-2008 10:57 AM

Quote:

Originally Posted by pixellany (Post 3254854)
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.. ;)

scls19fr 08-21-2008 12:48 PM

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 (Post 618105)
if you are on bash
Code:

# a=file.txt
# echo ${a/*./}
txt




All times are GMT -5. The time now is 05:13 PM.