0

I have files with multiple extension in a folder like .txt, .update, .text. The file names are like

out1.txt, out1.text, out1.update,

out2.txt, out2.text, out2.update

and so on....My command looks like this:

./script.pl out1.txt out1.text out1.update

I would like to put this command in the loop for every file. I have tried to use a loop like:

for i in *.{txt,text,update}

But it takes the full name including the extension so I couldn't figure out how to deduce the specific extension files in the command.

Thanks!

soosa
  • 125
  • 11
  • Possible duplicate of [Extract filename and extension in Bash](https://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash) – Joe Jul 07 '17 at 13:32
  • I checked it but it is something different. – soosa Jul 07 '17 at 13:36

2 Answers2

2

you can use :

for f in *.txt;
do
    b=${f%.txt} # the trick is there
    ./script.pl $b.txt $b.text $b.update
done
Setop
  • 2,262
  • 13
  • 28
  • It gives an error that "b" command is not found. I have through multiple testings but it is giving an error. – soosa Jul 07 '17 at 13:52
  • It worked. I had the space in between variable "b" and equal sign "=". I remove it and then it worked perfectly. Thanks! – soosa Jul 07 '17 at 14:04
  • @AsmaTahir, indeed, bash is sensitive to the space before and after "="; I fixed my answer. – Setop Jul 07 '17 at 14:44
  • Yeah, I noticed the space later that's why I removed it. Thanks once again! – soosa Jul 10 '17 at 08:01
1
ext=$(awk -F\. '{ print "."$NF }' <<< $i)
fil=$(basename -s $ext $i)

use awk to extract the extension (second delimited piece of data using ".") Use basename -s with the extension to the get the filename without the extension

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18