forked from e36freak/templates
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranspose_tmp
executable file
·54 lines (42 loc) · 1000 Bytes
/
transpose_tmp
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
#!/bin/awk -f
# this one reads the file once for each column, using a temp file
BEGIN {
FS = OFS = "|";
max = 0;
# create temp file
"mktemp" | getline tmp;
close("mktemp");
}
# keep track of the maximum number of fields seen
NF > max {
max = NF;
}
# on first read, deal with the first column and write to the temp file
{
if (NR == 1) {
printf("%s", $1);
} else {
printf("%s%s", OFS, $1);
}
print > tmp;
}
# at this point, we read the temp file again for columns 2 through max
END {
# print ORS for the end of the first line
print "";
# store the number of lines read
last = NR;
# close writing to the temp file
close(tmp);
# loop over columns 2 through max
for (c=2; c<=max; c++) {
# n stores the line number for each read
n = 0;
# read the temp file for each col
while ((getline < tmp) > 0) {
printf("%s%s", $c, ++n == last ? ORS : OFS);
}
# close the file, to start reading again next time
close(tmp);
}
}