Debian Codegraph @

gbconvtable

raw
1 #!/bin/sh
2 # gbconvtable ver. 5.6 Copyright (C) 2010-2015 Giulio Bottazzi
3
4 #default settings
5 pos=1
6 force="no"
7 dictfile=""
8
9 #read command line options; the position of the last option is saved
10 #in OPTIND
11 while getopts "d:c:f:h-:" opt
12 do
13 case $opt in
14 -)
15 case "${OPTARG}" in
16 help) help=yes;;
17 version) version=yes;;
18 esac;;
19 d) dictfile=$OPTARG;;
20 c) pos=$OPTARG;;
21 f) force=yes ; fstring=$OPTARG ;;
22 h) help=yes;;
23 \?) help=yes;;
24 esac
25 done
26
27 if [ "$version" = "yes" ]; then
28
29 cat - <<EOF
30 gbconvtable ver. 5.6
31
32 Copyright (C) 2010-2015 Giulio Bottazzi
33
34 This program is free software; you can redistribute it and/or
35 modify it under the terms of the GNU General Public License
36 (version 2) as published by the Free Software Foundation.
37
38 This program is distributed in the hope that it will be useful,
39 but WITHOUT ANY WARRANTY; without even the implied warranty of
40 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 GNU General Public License for more details.
42
43 Written by Giulio Bottazzi
44
45 Report bugs to <[email protected]>
46
47 Package home page <http://cafim.sssup.it/~giulio/software/gbutils/index.html>
48 EOF
49
50 exit
51 fi
52
53 if [ "$help" = "yes" ]; then
54
55 cat - <<EOF
56 Replace keys with values at a given position of the input file. The
57 name of the dictionary file is provided on the command line with the
58 option 'dictfile'. It is a simple text file organized in two
59 columns. The first column contains the keys and the second column the
60 respective values. Obviously the values can be equal, but the keys
61 should be all different. Data are read from standard input and all
62 fields at position 'pos' are considered keys of the provided
63 dictionary and replaced with the associated keys. If 'pos' is not
64 specified it is assumed equal to 1. If 'pos' is larger than the number
65 of fields, no replacement takes place. If the option 'force' is not
66 set, only those fields which appears as keys in the dictionary file
67 are replaced.
68
69 Usage: gbconvtable [options]
70
71 Options:
72 -d name of the dictionary file
73 -c position of the column of keys to be replaced
74 -f forced look-up: substitute provided string for non defined keys
75
76 Examples:
77 gbconvtable -d dict_file -c 3 -f 'none' < input_file
78
79 This program requires awk or gawk.
80 EOF
81
82 exit
83
84 fi
85
86
87 if ! [ -e "$dictfile" ]; then
88 echo "provide a dictionary file with option -d. try $0 -h for help"
89 exit
90 fi
91
92
93 awk -v dictfile=$dictfile -v pos=$pos -v force=$force -v fstring=$fstring '
94
95 BEGIN {
96
97 #open dictionary file and build the dictionary
98 while ((getline line < dictfile ) > 0){
99 split(line,pair)
100 dict[pair[1]]=pair[2]
101 }
102 }
103
104 {
105 #replace the field and print it
106 if(NF>=pos){
107 if($pos in dict)
108 $pos=dict[$pos]
109 else{
110 if(force=="yes")
111 $pos=fstring
112 }
113 }
114 print $0
115 }
116
117 '