-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
grebneke
  • 4,414
  • 17
  • 24

3 Answers3

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.

Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
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.

John1024
  • 109,961
  • 14
  • 137
  • 171
-1
for i in `ls *.ext`; do NAME=`echo $i | awk -F '.' '{print $1}'`; perl customise.pl $NAME.ext $NAME.new.psl; done
ishenkoyv
  • 665
  • 4
  • 9
  • 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 – user3263699 Feb 02 '14 at 20:37
  • 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. – Jonathan Leffler Feb 02 '14 at 20:41
  • Never use `for i on $(cmd)`. See http://stackoverflow.com/questions/19606864/ffmpeg-in-a-bash-pipe/19607361?stw=2#19607361 – Idriss Neumann Feb 02 '14 at 20:48
  • I agree with you. Zsolt Botykai answer is more appropriate – ishenkoyv Feb 02 '14 at 20:48
  • Thanks Idriss Neumann – ishenkoyv Feb 02 '14 at 20:49
  • Hi these are the file names gi_322422812_ref_NC_015111.1_.fsa.shIDs.ext gi_322422812_ref_NC_015111.1_.fsa.shIDs.new.psl and i have to run for every set like customise.pl gi_322422812_ref_NC_015111.1_.fsa.shIDs.new.psl gi_322422812_ref_NC_015111.1_.fsa.shIDs.ext . I tried the above ones but it is not working. Any idea? – user3263699 Feb 02 '14 at 21:15
  • Use Zsolt Botykai code for f in *.ext ; do perl customise.pl "${f}" "${f/%.ext/.new.psl}";done as my approach has several drawbacks, e.g. if filename contains several dots it doesn't work properly – ishenkoyv Feb 02 '14 at 21:17