-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmeta_process.sh
executable file
·108 lines (87 loc) · 2.12 KB
/
meta_process.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#! /usr/bin/env bash
set -o errexit -o errtrace -o pipefail
trap 'echo "Error on line ${LINENO}" >&2' ERR
BASEDIR=`dirname $0`
. $BASEDIR/meta_funcs.sh
usage() {
cat << _EOF_
Usage:
$0 [options] input_file output_file
where options are
-d default file to use if <input_file> does not exist you must still specify the input file
-p prefix for all META keys
-h display this message
_EOF_
}
parse_options() {
default_file=''
prefix=''
while getopts d:p:h arg "$@"
do
case "$arg" in
d)
default_file="$OPTARG"
[ -e "$default_file" ] || die 2 "default file '$default_file' does not exist"
[ -r "$default_file" ] || die 2 "default file '$default_file' is not readable"
;;
p)
prefix=$(echo "$OPTARG" | sed -e 's/\.*$//').
;;
h)
usage
exit 0
;;
?)
usage
exit 1
;;
esac
done
if [ $OPTIND -gt 1 ]; then
shift $(($OPTIND-1))
fi
if [ $# -lt 1 ]; then
echo "ERROR: Must specify input file"
usage
exit 1
fi
IN=$1
shift
if [ $# -lt 1 ]; then
echo "ERROR: Must specify output file"
usage
exit 1
fi
OUT=$1
shift
#psql_opts=$@
#echo "IN = '$IN', OUT = '$OUT'"
}
main() {
parse_options "$@"
if [ ! -e "$IN" -a -n "$default_file" ]; then
IN="$default_file"
# We've already verified $default_file exists and is readable
else
[ -e "$IN" ] || die 2 "input file '$IN' does not exist"
[ -r "$IN" ] || die 2 "input file '$IN' is not readable"
fi
SEP='#'
S='\'
SS='\\'
CLEAN="s${SEP}${S}${SEP}${SEP}${SS}${S}${SEP}${SEP}g"
declare -a replacements
searches=`egrep -o '@[A-Za-z0-9]+@' $IN | sort | uniq`
for search in $searches; do
key=`echo $search | tr -d @`
value=`getkey $prefix$key` || exit $?
echo " replacing $search with $value"
clean_search=`echo "$search" | sed -e "$CLEAN"` # -e "s/'/''/g"`
clean_value=`echo "$value" | sed -e "$CLEAN"` # -e "s/'/''/g"`
#echo "clean_search = '$clean_search', clean_value = '$clean_value'"
replacements+=(-e "s${SEP}$clean_search${SEP}$clean_value${SEP}g")
done
sed "${replacements[@]}" $IN > $OUT
}
main "$@"
# vi: expandtab ts=2 sw=2