I
I
Igor2014-03-12 13:49:04
linux
Igor, 2014-03-12 13:49:04

How to strip a file extension?

At the input, the file name, how to cut out its extension, if the name is: ..log.f, .log.ccc, log.qwerty, in general, pull it out to the right to the dot?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
EXL, 2014-03-12
@mmilky

#!/bin/bash
OLDIFS=$IFS
string=$1
IFS='.'
string_array=($string)
len=$(( ${#string_array[@]} - 1 ))
IFS=$OLDIFS
echo ${string_array[$len]}

exl@exl-Lenovo-G560e:~/SandBox > ./token.sh ..log.f
f
exl@exl-Lenovo-G560e:~/SandBox > ./token.sh .log.ccc
ccc
exl@exl-Lenovo-G560e:~/SandBox > ./token.sh log.qwerty
qwerty

A
axell1, 2014-04-13
@axell1

everything is simpler....
echo .log.ccc | awk-F\. '{print $NF}'
ccc
echo ..log.f | awk-F\. '{print $NF}'
f

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question