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!

2
  • Possible duplicate of Extract filename and extension in Bash Commented Jul 7, 2017 at 13:32
  • I checked it but it is something different. Commented Jul 7, 2017 at 13:36

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

4 Comments

It gives an error that "b" command is not found. I have through multiple testings but it is giving an error.
It worked. I had the space in between variable "b" and equal sign "=". I remove it and then it worked perfectly. Thanks!
@AsmaTahir, indeed, bash is sensitive to the space before and after "="; I fixed my answer.
Yeah, I noticed the space later that's why I removed it. Thanks once again!
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.