-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_xml.awk
53 lines (46 loc) · 956 Bytes
/
output_xml.awk
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
#!/usr/bin/awk -f
#
# DATEX - Textual database library for Shell Script
#
# Copyright (c) 2021 Flavio Augusto (@facmachado)
#
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE file for details.
#
function trim(x) {
sub(/^ */, "", x)
sub(/ *$/, "", x)
return x
}
BEGIN {
total = 0
fields = 0
}
$0 == "" {
next
}
!h[$1]++ {
fields++
}
{
value = ""
for (i = 3; i <= NF; i++) {
value = value " " $i
}
if ($1 == "id") {
line = line " <record>\n"
}
line = line " <" $1 ">" trim(value) "</" $1 ">\n"
if ($1 == "del") {
line = line " </record>\n"
}
total++
}
END {
total = total / fields
printf "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" \
"<result>\n" \
" <total>" total "</total>\n" \
" <records>\n" line " </records>\n" \
"</result>\n"
}