-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexshell2csv.awk
143 lines (131 loc) · 3.33 KB
/
exshell2csv.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
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
#!/usr/bin/env awk -f
BEGIN {
OFS=","
}
# field n and after n
function after(n, r) {
r="";
for(m=n;m<=NF;m++) r = r " " $m;
sub(" ", "", r);
return r;
}
# step ALPHABET counter (e.g. ABZ -> ACA)
function ALPH_advance(s, a, p) {
a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
p = length(s)
while (p != -1) {
match(a, substr(s, p, 1));
s = substr(s, 1, p-1) substr(a, RSTART+1, 1) substr(s, p+1, length(s));
match(s, "_")
p = RSTART - 1;
sub("_", "A", s);
if (p==0) {
s = "A" s;
break;
}
}
return s;
}
# n < m or not (n, m: ALPHABET counter)
function ALPH_lt(n, m) {
if (n=="") return 1;
if (length(n) > length(m)) return 0;
if (length(n) < length(m)) return 1;
for(l=1;n;l++) {
if(substr(n, 1, 1) == substr(m, 1, 1)) {
sub(".", "", n);
sub(".", "", m);
} else if (substr(n, 1, 1) < substr(m, 1, 1)) return 1; # success due to a letter
else return 0;
}
return 0; # n == m
}
# format serial number to Y/M/D/A
function fdate(n, y, m, d, a) {
# n: count of number ou dates since 1900
m = n;
n = n + 1900*365+475-19+5; # until 1900
n = n - 2; # 1900 is not a leap year (differ from Microsoft Excel)
y = sprintf("%d", n/365);
n = n - y * 365;
n = n - sprintf("%d", y / 4);
n = n + sprintf("%d", y / 100);
n = n - sprintf("%d", y / 400);
if (y%4==0&&(y%400==0||y%100!=0)) a = 1;
while (n <= 0) {
n = n + 365 + a;
y = y -1;
if (y%4==0&&(y%400==0||y%100!=0)) a = 1;
}
for(M=1;M<12;M++) {
if(M==2) d = 28 + a;
else if (M<8) {
if (M%2==0) d=30;
else d = 31;
} else {
if (M%2==0) d = 31;
else d = 30;
}
if (n > d) n = n - d;
else break;
}
a = m;
m = M;
d = n;
a = (a + 6) % 7;# sun is 0
return y"/"m"/"d"/"a;
}
# from sharedStrings.xml
$1=="l"{
str[NR-1] = after(2);
gsub("\\\\n", "\n", str[NR-1]);
gsub("\\\\\\\\", "\\\\", str[NR-1]);
}
# from sheet
$1!="l"{
if (ALPH_lt(ymax, $1)) ymax = $1;
if (xamx < $2) xmax = $2;
if ($3=="s") {# refer to sharedStrings.xml
cell[$2, $1] = str[$4];
}
############################ CUSTOMIZE AREA ############################
else if ($3==45||$3==40) {# time H:M
t=sprintf("%d", $4*24);
t=($4*24-t)*60;
t=sprintf("%d", t);
if (length(t)==1) t = "0"t;
cell[$2, $1] = sprintf("%d", $4*24)":"t;
}
else if ($3==16) {# date Y/M/D
cell[$2, $1] = fdate($4);
sub("/.$", "", cell[$2, $1]);
}
########################## END CUSTOMIZE AREA ##########################
else {# others
cell[$2, $1] = after(4);
}
}
#output CSV
END{
debug = 0; # add column and line numbers
if (debug) {
printf("%s,", 0);
for (n="A";ALPH_lt(n, ymax);n=ALPH_advance(n)) printf("%s,", n);
print ymax;
}
for (m=1;m<=xmax;m++) {
if (debug) printf("%s,", m);
outline = ""
for (n="A";ALPH_lt(n, ymax);n=ALPH_advance(n)) {
c = cell[m, n];
gsub("\"", "\"\"", c);
if (match(c, ",")||match(c, "\\n")||match(c, "\"")) outline = outline "\"" c "\"" OFS;
else outline = outline c OFS;
}
c = cell[m, ymax];
gsub("\"", "\"\"", c);
if (match(c, ",")||match(c, "\\n")||match(c, "\"")) outline = outline "\"" c "\"";
else outline = outline c;
print outline
}
}