#!/bin/bash

rm list-qsub-worked

for i in `cat FAILED-ERROR`;
#for i in xaaaaal
do

cp ${i}_build/${i} .

grep "Finished preparing" logs/stderr_${i} | awk '{print $3}' > names_worked

rm ${i}_worked
for j in `cat names_worked`
do
grep ${j} ${i} >> ${i}_worked
done

a=`tail -1 names_worked`
#echo ${a}
b=`grep -n ${a} ${i} | cut -f1 -d":"`
#echo ${b}
c=$((1000-${b}))
#echo ${c}

tail -${c} ${i} > ${i}_left
rm names_worked

cp qsub_${i}.csh qsub_${i}_worked.csh

sed -i "s/${i}/${i}_worked/g" qsub_${i}_worked.csh

echo "qsub_${i}_worked.csh" >> list-qsub-worked

rm ${i}

done
