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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
#!/usr/bin/mawk -We
# *********************************************************************
# Written by and copyright Carlo Strozzi <carlos@linux.it>.
#
# update: inserts/updates/deletes table rows based on the contents
# of an edit table.
# Copyright (C) 2000-2001 Carlo Strozzi <carlos@linux.it>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# 2001-01-03 Ported to NoSQL v3
# 2001-01-13 Added option '-w'
# 2001-03-20 'table_2' may be itself a Table Journal.
# 2001-04-18 Added inline help
# 2001-08-17 Added stdio portability
#
# $Id$
# *********************************************************************
BEGIN {
NULL = "" ; FS = OFS = "\t"; d_string = "..DEL.."
while (ARGV[++i] != NULL) {
if (ARGV[i] == "-d" || ARGV[i] == "--delete") d_string = ARGV[++i]
else if (ARGV[i] == "-N" || ARGV[i] == "--no-header") no_hdr = 1
else if (ARGV[i] == "-l" || ARGV[i] == "--last") pick_last = 1
else if (ARGV[i] == "-s" || ARGV[i] == "--stdin") swap = 1
else if (ARGV[i] == "-w" || ARGV[i] == "--write-size") s_file = ARGV[++i]
else if (ARGV[i] == "-h" || ARGV[i] == "--help") {
system("grep -v '^#' @DOCPATH@/update.txt")
rc = 1
exit(rc)
}
else if (ARGV[i] !~ /^-/) e_file = ARGV[i]
}
if (e_file == NULL) {
print "Usage: update [options] table_1 < table_2" > "@STDERR@"
exit(1)
}
#
# Load the associative array with the updates.
#
i = 0
if (swap) {
while (getline < "-" > 0) { # Read updates from stdin.
if (++j == 1) {
while (++i <= NF) {
if (pick_last) ep[$i] = i
else if (ep[$i] == NULL) ep[$i] = i
}
} else {
if (j > 2) {
edits[$1] = $0 # Edit array.
keys[$1] = 1 # Key array.
}
}
}
close(stdin) # Let's spare resources.
ARGV[1] = e_file # Set new stdin stream.
} else { # Read updates from e_file.
while (getline < e_file > 0) {
if (++j == 1) {
while (++i <= NF) {
if (pick_last) ep[$i] = i
else if (ep[$i] == NULL) ep[$i] = i
}
} else {
if (j > 2) {
edits[$1] = $0 # Edit array.
keys[$1] = 1 # Key array.
}
}
}
close(e_file) # Let's spare resources.
ARGV[1] = "-"
}
if (s_file != NULL) print j-2 > s_file
close(s_file)
NR = 0 # Reset record counter.
NF = 0
ARGC = 2 # Fix argv[].
edit_numfields = split($0, n) # No. of fields in edit table.
}
# At this point whichever file we have on stdin it is always
# the one that is being updated.
NR == 1 {
old_numfields = split($0, n) # No. of fields in main table.
if (!no_hdr) print
i = 0
while (++i <= NF) {
if (!P[$i]) {
if (i == 1) auto_col = $i
else auto_col = auto_col " " $i
}
if (pick_last) P[$i] = i
else {
if (!P[$i]) P[$i] = i
}
}
split(auto_col, c_names, " ")
next
}
# Dashline.
NR == 2 {
if (!no_hdr) { print; fflush() }
next
}
#
# Apply the updates to already existing keys.
#
split(edits[$1], a) { # Something to do ?
status[$1] = 1 # Mark key as updated.
if (a[2] == d_string) next # Skip this record.
printf("%s", a[1])
for (i = 2; i <= NF; i++) {
if (ep[c_names[i]]) repl = a[ep[c_names[i]]]
else {
#
# This is to cope with the case where 'table_2'
# itself contains delete strings in column 2,
# i.e. when we use 'update' against a Table Journal.
#
if ($i == d_string) $i = NULL
repl = $i
}
printf("\t%s", repl)
}
printf("\n") # Print record separator.
next
}
{ print }
END {
if (rc) exit(rc)
# Now handle new keys, i.e. edits referring to keys that do not
# occur in the file being updated. This is to cope with 'insert'
# operations.
for (j in keys) {
split(edits[j], a)
if (status[j] || a[2] == d_string) continue
printf("%s", a[1])
for (i = 2; i <= old_numfields; i++) {
if (ep[c_names[i]]) repl = a[ep[c_names[i]]]
else repl = NULL
printf("\t%s", repl)
}
printf("\n") # Print record separator.
}
}
#
# End of program.
#
|