-   Linux - Newbie (
-   -   Need help with wild card syntax to semi-automate file conversion using a perl script (

kmkocot 12-11-2007 08:04 PM

Need help with wild card syntax to semi-automate file conversion using a perl script
Hi all,

I have a set of files that contain structural data for related RNA molecules that I want to convert from one format to another using a perl script (part of the Vienna package if anyone's familiar). The perl script works like any other: infile.ct > outfile.seq

How can I replace the "infile" and "outfile" names I'm typing in when running the perl script with wildcards such that the name of the outfile will have the same name as the infile (except for the file extensions).


syg00 12-11-2007 08:15 PM

Wrap some scripting around it - something along these lines might work

for name in *.ct ; do $name.ct > $name.seq ; done

matthewg42 12-11-2007 08:29 PM

To remove a suffix from a variable when you use it, you can use this syntax:

So, for example:

for file in one.ct two.ct three.ct; do
    echo "original: $file ; no suffix: ${file%.ct} ; new suffix: ${file%.ct}.seq"

Should output:

original: one.ct ; no suffix: one ; new suffix: one.seq
original: two.ct ; no suffix: two ; new suffix: two.seq
original: three.ct ; no suffix: three ; new suffix: three.seq

So lets say you are working in a directory with a bunch of .ct files, you can do your processing like this:

for file in *.ct; do "$file" > "${file%.ct}.seq"

I quoted the file names in case any of the .ct files contain a space in the name - without the quotes it would not work correctly.

Note this will probably overwrite any existing .seq file which has a prefix which exists with a .ct extension. I say probably because the behaviour of the > redirection operator depends on the noclobber setting in the shell. If you want it so that existing files are not overwritten, you should first set noclobber:

set -o noclobber
When noclobber is set, re-directions which would otherwise replace existing files will fail.

syg00 12-11-2007 08:52 PM

Ahhhh - d'oh; thanks matthewg42.

Ignore mine - musta been asleep.

sundialsvcs 12-11-2007 08:54 PM

Good advice. There are two ways to approach this problem: one is to "let the shell do it," and the other is to "let Perl do it." Which one you choose is simply whichever one you're most comfortable with.

kmkocot 12-12-2007 02:55 PM

Thanks! Got it! Really appreciate the help!

All times are GMT -5. The time now is 03:41 AM.