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

Contents of /manual/tools/replace_line_nb

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


Revision 1.1 - (show annotations) (download)
Mon Aug 8 20:46:53 2005 UTC (18 years, 8 months ago) by jmc
Branch: MAIN
small script to put Line Number (from MITgcm source file) into tex file

1 #!/bin/sh
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}'
9 echo 'LIMITATION: a) no space / blank between PUT_LINE_NB and "="'
10 echo ' b) no space in the variable name'
11 echo ' (e.g.: "#YYY=" works, but "# YYY=" does not)'
12 exit
13 else
14 texFil=$1
15 inpFil=$2
16 fi
17
18 outFil=`echo $texFil.tex | sed 's/.templ//'`
19 tmpFil1=TTT_fil1.$$
20 tmpFil2=TTT_fil2.$$
21 dbug=
22
23 #texFil='inp_tst'
24 #outFil=tata
25 #tmpFil1=titi
26 #tmpFil2=toto
27 #dbug=1
28
29 rm -f $outFil $tmpFil1 $tmpFil2
30 cp -f $texFil $outFil
31
32 # take all the lines in texFil where a line-Nb needs to be added:
33 listR=`egrep 'PUT_LINE_NB:' $texFil`
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 while test $pp != $xx
46 do
47 pp=$xx
48 xx=`echo $pp | sed 's/=./=/g'`
49 done
50 xx=`echo $pp | sed 's/=$//g'`
51 if test $dbug ; then echo ' Var name: xx='$xx ; fi
52 #- select the line in texFil that has been copied from inpFil
53 # (and needs to receive a line Nb)
54 egrep $xx $texFil | grep -v 'PUT_LINE_NB:' > $tmpFil1
55 if test -s $tmpFil1
56 then
57 #echo 'found' $xx 'in file:' $texFil
58 out=1;
59 #- remove space at the beginning of the line:
60 while test $out = 1
61 do
62 mv $tmpFil1 $tmpFil2
63 sed 's/^ //g' $tmpFil2 > $tmpFil1
64 diff $tmpFil1 $tmpFil2 > /dev/null
65 out=$?
66 done
67 yy=$xx
68 nbT=`egrep -c "^${yy}" $tmpFil1`
69 if [ $nbT -gt 1 ]
70 then
71 #- try to search for "xx=":
72 #- count number of blank between variable name "xx" and "=":
73 nblk=0
74 out=`egrep -c "^${yy}=" $tmpFil1`
75 while test $out = 0
76 do
77 yy="${yy} " ; #echo "yy=${yy}<"
78 nblk=`expr $nblk + 1` ;
79 out=`egrep -c "^${yy}=" $tmpFil1`
80 if [ $nblk -ge 100 ] ; then out=1 ; fi
81 done
82 if test $nblk = 100 ; then yy=$xx ; else yy=${yy}"=" ; fi
83 nbT=`egrep -c "^${yy}" $tmpFil1`
84 if test $dbug ; then echo 'nblk='$nblk ', nbT='$nbT ", yy=>${yy}<" ; fi
85 fi
86 else
87 nbT=0
88 fi
89 nn='XXX';
90 if test $nbT = '1' ; then
91 #- now take the only line that start with variable name "xx":
92 LINE=`egrep "^${yy}" $tmpFil1 | sed 's/\*/\\\*/g'`
93 if test $dbug ; then echo ' LINE='$LINE ; fi
94 #- find the same line in inpFil and take the line number (nn):
95 nbI=`egrep -c "$LINE" $inpFil`
96 #echo 'nbI='$nbI
97 if test $nbI = 1
98 then
99 nn=`sed -n "/$LINE/=" $inpFil`
100 else
101 if test $nbI = '0';
102 then echo 'no line found with "'$xx'" in file:' $inpFil
103 else echo 'too many('$nbI') lines with "'$xx'" in file:' $inpFil
104 fi
105 fi
106 else
107 if test $nbT = '0';
108 then echo 'did not find "'$xx'" in file:' $texFil
109 else echo 'too many('$nbT') "'$xx'" in file:' $texFil
110 fi
111 fi
112 #- replace by the line number (if found), XXX otherwise:
113 sed "s/PUT_LINE_NB:${xx}=/$nn/" $outFil > $tmpFil1
114 if test $dbug ; then echo " Replace PUT_LINE_NB:${xx}= by line number: nn=$nn" ; fi
115 #if test $dbug ; then echo diff $outFil $tmpFil1 ; diff $outFil $tmpFil1 ; fi
116 mv $tmpFil1 $outFil
117 fi
118 done
119 rm -f $tmpFil1 $tmpFil2
120 if test $dbug ; then echo diff $outFil $texFil ; diff $outFil $texFil ; fi
121
122 exit

  ViewVC Help
Powered by ViewVC 1.1.22