LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using multiple files in a loop in bash shell (https://www.linuxquestions.org/questions/linux-newbie-8/using-multiple-files-in-a-loop-in-bash-shell-4175609375/)

Asoo 07-07-2017 08:01 AM

Using multiple files in a loop in bash shell
 
Greetings!

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:
Code:

./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:

Code:

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 in advance!

scasey 07-07-2017 11:24 AM

Quote:

Originally Posted by Asoo (Post 5731872)
I would like to put this command in the loop for every file. I have tried to use a loop like:

Code:

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 in advance!

Looks like
Code:

for i in {txt,text,update}
would work to have only the extensions in the loop.

Is that what you wanted?

Turbocapitalist 07-07-2017 01:19 PM

Quote:

Originally Posted by Asoo (Post 5731872)
Code:

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.

That globbing *.{txt,text,update} reads the file names into the variable i one at a time.

If you do separate out everything after the final dot in the file name using only in bash, ksh, or zsh:

Code:

for i in *.txt; do
        f=${i%.*};
        e=${i##*.};
        echo $f $e;
done;


Asoo 07-10-2017 03:04 AM

Thank you for the help.

I did something like this and it worked:

Quote:

b=${f%.txt}
./script.pl $b.txt $b.text $b.update
Thanks!


All times are GMT -5. The time now is 09:02 PM.