| 1 |
#! /usr/bin/env bash |
| 2 |
|
| 3 |
if test $# != 2 |
| 4 |
then |
| 5 |
echo "Usage:"`basename $0`" {tex_file.templ} {data_file} :" |
| 6 |
echo " replace >>>PUT_LINE_NB:{variable_name}=<<< in file {tex_file.templ}" |
| 7 |
echo " with line number in file {data_file} that corresponds to the" |
| 8 |
echo " description of variable {variable_name} in {tex_file.templ}" |
| 9 |
echo "LIMITATION: a) no space (blank) between 'PUT_LINE_NB:' and variable name" |
| 10 |
echo " b) no space (blank) in the variable name (but OK before '=')" |
| 11 |
echo " (e.g.: '#YYY =' works, but '# YYY=' does not)" |
| 12 |
exit |
| 13 |
else |
| 14 |
texFil=$1 |
| 15 |
inpFil=$2 |
| 16 |
fi |
| 17 |
if test ! -f $texFil ; then echo " ERROR: file='$texFil' not found" ; exit ; fi |
| 18 |
if test ! -f $inpFil ; then echo " ERROR: file='$inpFil' not found" ; exit ; fi |
| 19 |
|
| 20 |
outFil=`echo $texFil.tex | sed 's/.templ//'` |
| 21 |
tmpFil1=TTT_fil1.$$ |
| 22 |
tmpFil2=TTT_fil2.$$ |
| 23 |
dbug= |
| 24 |
|
| 25 |
#texFil='inp_tst' ; outFil=tata ; tmpFil1=titi ; tmpFil2=toto |
| 26 |
#dbug=1 |
| 27 |
|
| 28 |
rm -f $outFil $tmpFil1 $tmpFil2 |
| 29 |
cp -f $texFil $outFil |
| 30 |
sed 's/ *//g' $inpFil > $tmpFil2 |
| 31 |
|
| 32 |
# take all the lines in texFil where a line-Nb needs to be added: |
| 33 |
listR=`grep 'PUT_LINE_NB:' $texFil | sed 's/PUT_LINE_NB:/ PUT_LINE_NB:/g'` |
| 34 |
if test $dbug ; then echo $listR ; fi |
| 35 |
|
| 36 |
for pp in $listR |
| 37 |
do |
| 38 |
#echo 'pp='$pp |
| 39 |
xx=`echo $pp | sed "s/PUT_LINE_NB://g"` |
| 40 |
#echo 'xx='$pp |
| 41 |
if test $pp != $xx |
| 42 |
then |
| 43 |
#- select the variable name (=xx) to look for (in inpFil & texFil): |
| 44 |
if test $dbug ; then echo ' xx='$xx ' <- from pp='$pp ; fi |
| 45 |
xx=`echo $xx | sed 's/=.*$//g'` |
| 46 |
if test $dbug ; then echo ' Var name: xx='$xx ; fi |
| 47 |
#- select the line in texFil that has been copied from inpFil |
| 48 |
# (and needs to receive a line Nb) |
| 49 |
grep $xx $texFil | grep -v 'PUT_LINE_NB:' | sed 's/^ *//g' > $tmpFil1 |
| 50 |
if test -s $tmpFil1 |
| 51 |
then |
| 52 |
#echo 'found' $xx 'in file:' $texFil |
| 53 |
yy=$xx |
| 54 |
nbT=`grep -c "^${yy}" $tmpFil1` |
| 55 |
if [ $nbT -gt 1 ] |
| 56 |
then |
| 57 |
#- try to search for "xx=": |
| 58 |
yy="${xx} *=" |
| 59 |
nbT=`grep -c "^${yy}" $tmpFil1` |
| 60 |
if test $dbug ; then echo " nbT='$nbT', yy='${yy}'" ; fi |
| 61 |
fi |
| 62 |
else |
| 63 |
nbT=0 |
| 64 |
fi |
| 65 |
nn='XXX'; |
| 66 |
if test $nbT = '1' ; then |
| 67 |
#- now take the only line that start with variable name "xx": |
| 68 |
LINE=`sed -n "/^${yy}/p" $tmpFil1 | sed 's/\*/\\\*/g'` |
| 69 |
#- find the same line in inpFil and take the line number (nn): |
| 70 |
inpUsed=$inpFil |
| 71 |
nbI=`grep -c "$LINE" $inpUsed` |
| 72 |
if test $dbug ; then echo " LINE='$LINE' ; nbI='$nbI'" ; fi |
| 73 |
if test $nbI = '0' ; then |
| 74 |
#- try without blank: |
| 75 |
LINE=`sed -n "/^${yy}/p" $tmpFil1 | sed 's/\*/\\\*/g' | sed 's/ *//g'` |
| 76 |
inpUsed=$tmpFil2 |
| 77 |
nbI=`grep -c "$LINE" $inpUsed` |
| 78 |
if test $dbug ; then echo " LINE='$LINE' ; nbI='$nbI'" ; fi |
| 79 |
fi |
| 80 |
if test $nbI = 1 |
| 81 |
then |
| 82 |
nn=`sed -n "/$LINE/=" $inpUsed` |
| 83 |
else |
| 84 |
if test $nbI = '0'; |
| 85 |
then echo "no line '$LINE' found in file:" $inpFil |
| 86 |
else echo 'too many('$nbI') lines with "'$xx'" in file:' $inpFil |
| 87 |
fi |
| 88 |
fi |
| 89 |
else |
| 90 |
if test $nbT = '0'; |
| 91 |
then echo 'did not find "'$xx'" in file:' $texFil |
| 92 |
else echo 'too many('$nbT') "'$xx'" in file:' $texFil |
| 93 |
fi |
| 94 |
fi |
| 95 |
#- replace by the line number (if found), XXX otherwise: |
| 96 |
sed "s/PUT_LINE_NB:${xx} *=/$nn/" $outFil > $tmpFil1 |
| 97 |
if test $dbug ; then echo " Replace PUT_LINE_NB:${xx}= by line number: nn=$nn" ; fi |
| 98 |
mv $tmpFil1 $outFil |
| 99 |
fi |
| 100 |
done |
| 101 |
rm -f $tmpFil1 $tmpFil2 |
| 102 |
if test $dbug ; then echo diff $outFil $texFil ; diff $outFil $texFil ; fi |
| 103 |
|
| 104 |
exit |