source: branches/bibliolocean/src/add_loceanbibid.sh @ 226

Last change on this file since 226 was 187, checked in by pinsard, 12 years ago

add loceanbibid and timestamp

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Id URL
File size: 3.4 KB
Line 
1#! /bin/sh
2#+
3#
4# ==================
5# add_loceanbibid.sh
6# ==================
7#
8# SYNOPSIS
9# ========
10#
11# ``add_loceanbibid.sh -i ifile -o ofile``
12#
13# DESCRIPTION
14# ===========
15#
16# add a field loceanbibid to each reference
17#
18# -i  input file (ifile)
19# -o  output file
20#
21# We assume here that the input file is written with this pattern of block::
22#
23#   @reftyp{Id
24#   ...
25#   }
26#
27# EXAMPLES
28# ========
29#
30# To add loceanid in each ref of the biblio of LOCEAN::
31#
32#  $ add_loceanbibid.sh -i biblioref.bib -o biblioref_withid.bib
33#
34# Check can be made ::
35#
36#  sed -e "/loceanbibid={.*}/d" biblioref_withid.bib > ginette
37#  diff biblioref.bib ginette
38#
39# Output contains lines which are before the first block, after the last
40# block and lines between blocks.
41#
42# PREAMBLE and comment blocks are preserve.
43#
44# TODO
45# ====
46#
47# check for dupe loceanbibid
48#
49# remove timestap hard coded value
50#
51# EVOLUTIONS
52# ==========
53#
54# $Id$
55#
56# - fplod 20120419
57#
58#   * creation
59#
60#-
61#
62system=$(uname)
63case "${system}" in
64    AIX|IRIX64)
65        echo " www : no specific posix checking"
66    ;;
67    *)
68        set -o posix
69    ;;
70esac
71unset system
72#
73LANG=POSIX
74#
75set -u
76#
77action=$(basename ${0} .sh)
78command=$(basename ${0})
79log_date=$(date -u +"%Y%m%dT%H%M%SZ")
80#
81usage="Usage : ${command} -i ifile -o ofile"
82#
83hostname=$(hostname)
84#
85# default
86# N.A.
87#
88minargcount=4
89if [ ${#} -lt ${minargcount} ]
90then
91   echo "eee : not enought arguments"
92   echo "${usage}"
93   exit 1
94fi
95#
96while [ ${#} -gt 0 ]
97do
98    case ${1} in
99         -i)
100            ifile=${2}
101            shift
102         ;;
103      -o)
104         ofile=${2%/}
105         shift
106      ;;
107      -h)
108         echo "${usage}"
109         exit 0
110      ;;
111      *)
112         # anything else
113         echo "eee : unknown option ${1}"
114         echo "eee : ${usage}"
115         exit 1
116      ;;
117   esac
118   # next flag
119   shift
120done
121#
122#
123# check for ${PROJECT_LOG}
124checkprojectlog
125status=${?}
126if [ ${status} -ne 0 ]
127then
128    echo "${command} : eee : pb with ${PROJECT_LOG}"
129    exit 1
130fi
131unset status
132#
133# check for ifile
134if [ ! -r ${ifile} ]
135then
136    echo "eee : ${ifile} not accessible"
137    exit 1
138fi
139#
140# check for ofile
141# the idea is to prevent unwanted overwrite
142if [ -f ${ofile} ]
143then
144    echo "eee : ${ofile} already exist"
145    exit 1
146fi
147#
148log=$(buildlogfile ${action} ${log_date})
149echo "[Context]" 1>> ${log}
150echo "command=$(basename ${0})" 1>>${log}
151echo "hostname=${hostname}" 1>> ${log}
152echo "runtime=${log_date}" 1>> ${log}
153echo "log=${log}" 1>> ${log}
154unset log_date
155echo "" 1>> ${log}
156#
157# get the number of ref.
158#
159nbref=$(grep -c '^@.*{.*,$' ${ifile})
160#
161# remove one because of @PREAMBLE
162nbref=$(( ${nbref} - 1))
163echo "${command} : iii : nbref= ${nbref}" >> ${log} 2>&1
164echo "${command} : iii : nbref= ${nbref}"
165awkfile=${PROJECT_LOG}/add_loceanid${$}.awk
166cat > ${awkfile} << EOF
167BEGIN{
168    nbref=${nbref}
169    iref=${nbref}+1
170}
171{
172if (index(\$0,"@")==1) {
173    if (index(\$0,"@PREAMBLE")!=1) {
174        print \$0
175        printf "  loceanbibid={%5.5d},\n",iref
176        printf "  timestamp={20120419},\n",iref
177        iref=iref-1;
178    }
179    else {
180        print \$0
181    }
182    }
183else {
184        print \$0
185    }
186}
187EOF
188#
189#more ${awkfile}
190# apply awk command
191awk -f  ${awkfile} ${ifile} > ${ofile} 2>&1
192status=${?}
193if [ ${?} -ne 0 ]
194then
195    echo "${command} : eee : pb with awk command"
196    exit 1
197fi
198#
199echo "iii : ${ifile} has been completed with loceanbibid in ${ofile}"
200#
201# clean
202rm ${awkfile}
203unset ifile
204unset ofile
205#
206# end
207exit 0
Note: See TracBrowser for help on using the repository browser.