/[MITgcm]/manual/tools/replace_line_nb
ViewVC logotype

Annotation of /manual/tools/replace_line_nb

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (hide annotations) (download)
Sun May 8 15:10:45 2011 UTC (12 years, 10 months ago) by jmc
Branch: MAIN
CVS Tags: checkpoint01, HEAD
Changes since 1.1: +36 -54 lines
less strick on exact number of blanks in data-file

1 jmc 1.2 #! /usr/bin/env bash
2 jmc 1.1
3     if test $# != 2
4     then
5 jmc 1.2 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 jmc 1.1 exit
13     else
14     texFil=$1
15     inpFil=$2
16     fi
17 jmc 1.2 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 jmc 1.1
20     outFil=`echo $texFil.tex | sed 's/.templ//'`
21     tmpFil1=TTT_fil1.$$
22     tmpFil2=TTT_fil2.$$
23     dbug=
24    
25 jmc 1.2 #texFil='inp_tst' ; outFil=tata ; tmpFil1=titi ; tmpFil2=toto
26 jmc 1.1 #dbug=1
27    
28     rm -f $outFil $tmpFil1 $tmpFil2
29     cp -f $texFil $outFil
30 jmc 1.2 sed 's/ *//g' $inpFil > $tmpFil2
31 jmc 1.1
32     # take all the lines in texFil where a line-Nb needs to be added:
33 jmc 1.2 listR=`grep 'PUT_LINE_NB:' $texFil | sed 's/PUT_LINE_NB:/ PUT_LINE_NB:/g'`
34 jmc 1.1 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 jmc 1.2 xx=`echo $xx | sed 's/=.*$//g'`
46 jmc 1.1 if test $dbug ; then echo ' Var name: xx='$xx ; fi
47 jmc 1.2 #- select the line in texFil that has been copied from inpFil
48 jmc 1.1 # (and needs to receive a line Nb)
49 jmc 1.2 grep $xx $texFil | grep -v 'PUT_LINE_NB:' | sed 's/^ *//g' > $tmpFil1
50 jmc 1.1 if test -s $tmpFil1
51     then
52     #echo 'found' $xx 'in file:' $texFil
53     yy=$xx
54 jmc 1.2 nbT=`grep -c "^${yy}" $tmpFil1`
55     if [ $nbT -gt 1 ]
56 jmc 1.1 then
57 jmc 1.2 #- 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 jmc 1.1 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 jmc 1.2 LINE=`sed -n "/^${yy}/p" $tmpFil1 | sed 's/\*/\\\*/g'`
69 jmc 1.1 #- find the same line in inpFil and take the line number (nn):
70 jmc 1.2 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 jmc 1.1 if test $nbI = 1
81     then
82 jmc 1.2 nn=`sed -n "/$LINE/=" $inpUsed`
83 jmc 1.1 else
84     if test $nbI = '0';
85 jmc 1.2 then echo "no line '$LINE' found in file:" $inpFil
86 jmc 1.1 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 jmc 1.2 sed "s/PUT_LINE_NB:${xx} *=/$nn/" $outFil > $tmpFil1
97 jmc 1.1 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

  ViewVC Help
Powered by ViewVC 1.1.22