0

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.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
CrazyC
  • 1,840
  • 6
  • 39
  • 60
  • 1
    Did you try `test=${filename%%.*}`? `%%` returns the longest matching pattern. – Fazlin Sep 30 '14 at 06:30
  • But the command you tried should have removed the `.old` from your string and resulted in `TEST_VSS_TT.csv` instead of throwing the error, Have you double checked your command ? , – Ram Sep 30 '14 at 06:40
  • My first problem is still getting same error even with %%. My system is Solaris sparc. – CrazyC Sep 30 '14 at 06:44

4 Answers4

0

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.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

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

Alexandre Halm
  • 979
  • 1
  • 8
  • 18
0

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
akira
  • 11
  • 1
0

echo "Test_VSS_TT.csv.old"| awk -F"." '{print $1}'

dev
  • 732
  • 2
  • 8
  • 29