#! /usr/bin/env bash if test $# != 2 then echo "Usage:"`basename $0`" {tex_file.templ} {data_file} :" echo " replace >>>PUT_LINE_NB:{variable_name}=<<< in file {tex_file.templ}" echo " with line number in file {data_file} that corresponds to the" echo " description of variable {variable_name} in {tex_file.templ}" echo "LIMITATION: a) no space (blank) between 'PUT_LINE_NB:' and variable name" echo " b) no space (blank) in the variable name (but OK before '=')" echo " (e.g.: '#YYY =' works, but '# YYY=' does not)" exit else texFil=$1 inpFil=$2 fi if test ! -f $texFil ; then echo " ERROR: file='$texFil' not found" ; exit ; fi if test ! -f $inpFil ; then echo " ERROR: file='$inpFil' not found" ; exit ; fi outFil=`echo $texFil.tex | sed 's/.templ//'` tmpFil1=TTT_fil1.$$ tmpFil2=TTT_fil2.$$ dbug= #texFil='inp_tst' ; outFil=tata ; tmpFil1=titi ; tmpFil2=toto #dbug=1 rm -f $outFil $tmpFil1 $tmpFil2 cp -f $texFil $outFil sed 's/ *//g' $inpFil > $tmpFil2 # take all the lines in texFil where a line-Nb needs to be added: listR=`grep 'PUT_LINE_NB:' $texFil | sed 's/PUT_LINE_NB:/ PUT_LINE_NB:/g'` if test $dbug ; then echo $listR ; fi for pp in $listR do #echo 'pp='$pp xx=`echo $pp | sed "s/PUT_LINE_NB://g"` #echo 'xx='$pp if test $pp != $xx then #- select the variable name (=xx) to look for (in inpFil & texFil): if test $dbug ; then echo ' xx='$xx ' <- from pp='$pp ; fi xx=`echo $xx | sed 's/=.*$//g'` if test $dbug ; then echo ' Var name: xx='$xx ; fi #- select the line in texFil that has been copied from inpFil # (and needs to receive a line Nb) grep $xx $texFil | grep -v 'PUT_LINE_NB:' | sed 's/^ *//g' > $tmpFil1 if test -s $tmpFil1 then #echo 'found' $xx 'in file:' $texFil yy=$xx nbT=`grep -c "^${yy}" $tmpFil1` if [ $nbT -gt 1 ] then #- try to search for "xx=": yy="${xx} *=" nbT=`grep -c "^${yy}" $tmpFil1` if test $dbug ; then echo " nbT='$nbT', yy='${yy}'" ; fi fi else nbT=0 fi nn='XXX'; if test $nbT = '1' ; then #- now take the only line that start with variable name "xx": LINE=`sed -n "/^${yy}/p" $tmpFil1 | sed 's/\*/\\\*/g'` #- find the same line in inpFil and take the line number (nn): inpUsed=$inpFil nbI=`grep -c "$LINE" $inpUsed` if test $dbug ; then echo " LINE='$LINE' ; nbI='$nbI'" ; fi if test $nbI = '0' ; then #- try without blank: LINE=`sed -n "/^${yy}/p" $tmpFil1 | sed 's/\*/\\\*/g' | sed 's/ *//g'` inpUsed=$tmpFil2 nbI=`grep -c "$LINE" $inpUsed` if test $dbug ; then echo " LINE='$LINE' ; nbI='$nbI'" ; fi fi if test $nbI = 1 then nn=`sed -n "/$LINE/=" $inpUsed` else if test $nbI = '0'; then echo "no line '$LINE' found in file:" $inpFil else echo 'too many('$nbI') lines with "'$xx'" in file:' $inpFil fi fi else if test $nbT = '0'; then echo 'did not find "'$xx'" in file:' $texFil else echo 'too many('$nbT') "'$xx'" in file:' $texFil fi fi #- replace by the line number (if found), XXX otherwise: sed "s/PUT_LINE_NB:${xx} *=/$nn/" $outFil > $tmpFil1 if test $dbug ; then echo " Replace PUT_LINE_NB:${xx}= by line number: nn=$nn" ; fi mv $tmpFil1 $outFil fi done rm -f $tmpFil1 $tmpFil2 if test $dbug ; then echo diff $outFil $texFil ; diff $outFil $texFil ; fi exit