-2

I have thousands of two set of files, one with name.ext and another set for the same name ending with name.new.psl. So for every name.ext there is a name.new.psl. Now I have to pass this as arguments to a script such as customise.pl name.ext name.new.psl

Any ideas for a loop in bash? The first name is common for each name.ext and name.new.psl like:

perl customise.pl name.ext name.new.psl
1

3 Answers 3

4
for f in *.ext ; do
    perl customise.pl "${f}" "${f/%.txt/.new.psl}"
done

Will do it for you in the current working directory.

Sign up to request clarification or add additional context in comments.

3 Comments

Is the % in ${f/%.txt/.new.psl} conflating the ${f%.txt} notation with ${f/.txt/.new}?
No. The % is used to anchor the pattern to the end of the string, similar to the regular expression \.txt$.
@JonathanLeffle chepner is right. IMO it's more reliable to match extensions this way.
2
for fname in *.ext
do
    perl customise.pl "$fname" "${fname%.ext}.new.psl"
done

The above does not require any special bash features. So, it is compatible with, for example, dash which is the default shell (/bin/sh) on debian-derived distributions.

The trick above is that ${fname%.ext} tells the shell to remove the text .ext from the end of $fname, leaving just the "name" part. Thus, "${fname%.ext}.new.psl" removes .ext adds the .new.psl extension.

The file names in the code above are in double-quotes. This is so that this script will work even if the file names have spaces in them.

Comments

-1
for i in `ls *.ext`; do NAME=`echo $i | awk -F '.' '{print $1}'`; perl customise.pl $NAME.ext $NAME.new.psl; done

7 Comments

Hi the file names are like this and i got error busing suggested bashgi_322422812_ref_NC_015111.1_.fsa.shIDs.ext gi_322422812_ref_NC_015111.1_.fsa.shIDs.new.psl
Using $(ls *.ext) in place of *.ext is definitely seriously inferior. The globbing notation *.ext manages with spaces and other special characters in the file names; the $(ls *.ext) notation breaks on file names containing spaces etc.
I agree with you. Zsolt Botykai answer is more appropriate
Thanks Idriss Neumann
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.