I suppose to strip down a substring in my shell script. I am trying as follows:
fileName="Test_VSS_TT.csv.old"
here i want to remove the string ".csv.old" and my
test=${fileName%.*}
but getting bad substitution error.
I suppose to strip down a substring in my shell script. I am trying as follows:
fileName="Test_VSS_TT.csv.old"
here i want to remove the string ".csv.old" and my
test=${fileName%.*}
but getting bad substitution error.
Here you go,
$ echo $f
Test_VSS_TT.csv.old
$ test=${f%%.*}
$ echo $test
Test_VSS_TT
%%
will do a longest match. So it matches from the first dot upto the last and then removes the matched characters.
you are looking for test=${filename%%.*}
the doc for parameter expansion in bash here and in zsh here
%.*
will match the first .*
pattern, whereas %%.*
will match the longest one
[edit]
if sed
is available, you could try something like that : echo "filename.txt.bin" | sed "s/\..*//g"
which yields filename
If your intention is to extract file name without extension, then how about this?
$ echo ${fileName}
Test_VSS_TT.csv.old
$ test=`echo ${fileName} |cut -d '.' -f1`
$ echo $test
Test_VSS_TT