LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help with wild card syntax to semi-automate file conversion using a perl script (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-with-wild-card-syntax-to-semi-automate-file-conversion-using-a-perl-script-606153/)

kmkocot 12-11-2007 07: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: ct2b.pl 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).

Thanks!
Kevin

syg00 12-11-2007 07:15 PM

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

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

matthewg42 12-11-2007 07:29 PM

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

${variablename%suffix}
So, for example:
Code:

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

Should output:
Code:

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

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

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

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

syg00 12-11-2007 07:52 PM

Ahhhh - d'oh; thanks matthewg42.

Ignore mine - musta been asleep.

sundialsvcs 12-11-2007 07: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 01:55 PM

Thanks! Got it! Really appreciate the help!


All times are GMT -5. The time now is 10:59 AM.