LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-11-2007, 07:04 PM   #1
kmkocot
Member
 
Registered: Dec 2007
Location: Tuscaloosa, AL
Posts: 126

Rep: Reputation: 15
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
 
Old 12-11-2007, 07:15 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Wrap some scripting around it - something along these lines might work
Code:
for name in *.ct ; do ct2b.pl $name.ct > $name.seq ; done
 
Old 12-11-2007, 07:29 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 12-11-2007, 07:52 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Ahhhh - d'oh; thanks matthewg42.

Ignore mine - musta been asleep.
 
Old 12-11-2007, 07:54 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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.
 
Old 12-12-2007, 01:55 PM   #6
kmkocot
Member
 
Registered: Dec 2007
Location: Tuscaloosa, AL
Posts: 126

Original Poster
Rep: Reputation: 15
Thanks! Got it! Really appreciate the help!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Speaking Unix, Part 6: Automate, automate, automate! LXer Syndicated Linux News 0 01-04-2007 09:54 AM
[Perl] basic byte conversion script noir911 Programming 1 11-21-2006 01:01 AM
Perl file conversion results in a truncated file kshaffer Programming 0 01-31-2005 02:31 PM
bash script gone wild ipconfig Linux - Newbie 2 12-10-2003 02:53 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration