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