shell scripting: search/replace & check file exist

Posted by johndashen on Stack Overflow See other posts from Stack Overflow or by johndashen
Published on 2010-04-16T23:09:52Z Indexed on 2010/04/17 0:03 UTC
Read the original article Hit count: 417

I have a perl script (or any executable) E which will take a file foo.xml and write a file foo.txt. I use a Beowulf cluster to run E for a large number of XML files, but I'd like to write a simple job server script in shell (bash) which doesn't overwrite existing txt files.

I'm currently doing something like

#!/bin/sh
PATTERN="[A-Z]*0[1-2][a-j]"; # this matches foo in all cases 
todo=`ls *.xml | grep $PATTERN -o`;
isdone=`ls *.txt | grep $PATTERN -o`;

whatsleft=todo - isdone; # what's the unix magic?

#tack on the .xml prefix with sed or something

#and then call the job server; 
jobserve E "$whatsleft";

and then I don't know how to get the difference between $todo and $isdone. I'd prefer using sort/uniq to something like a for loop with grep inside, but I'm not sure how to do it (pipes? temporary files?)

As a bonus question, is there a way to do lookahead search in bash grep?

To clarify:

so the simplest way to do what i'm asking is (in pseudocode)

for i in `/bin/ls *.xml`
do
   replace xml suffix with txt
   if [that file exists]
      add to whatsleft list
   end
done

© Stack Overflow or respective owner

Related posts about shell

Related posts about scripting