-
Notifications
You must be signed in to change notification settings - Fork 3
/
col_div2.awk
executable file
·92 lines (84 loc) · 3.06 KB
/
col_div2.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
#!/usr/bin/awk -f
function synopsis_show() {
printf("\n");
printf("NAME\n");
printf("\n");
printf("\tcol_div2.awk\n");
printf("\n");
printf("SYNOPSIS\n");
printf("\n");
printf("\tawk -f col_div2.awk\t\t\t\t\\\n");
printf("\t\t\t [-v width=<width>\t\t\\\n");
printf("\t\t\t -v precision=<precision>]\t\\\n");
printf("\t\t\t -v width=<cellWidth>]\t\t\\\n");
printf("\t\t\t -v start=<colStart>]\t\t\\\n");
printf("\t\t\t -v preserve=<colPreserve>]\t\\\n");
printf("\t\t\t -v flip=<1>]\t\t\t\\\n");
printf("\t\t\t <fileToProcess>\n");
printf("\n");
printf("DESCRIPTION\n");
printf("\n");
printf("\t'col_div2.awk' is a simple awk script that accepts as input\n");
printf("\ta multi-column array of numbers, and returns col[n]/col[n+m/2]\n");
printf("\twhere 'm' is the total number of columns in the original input.\n");
printf("\tObvioulsy, 'm' must be even.\n");
printf("\n");
printf("\tThe <colStart> specifies the start value of col[n], and the\n");
printf("\t<colPreserve> indicates a col index to 'preserve' and print first.\n");
printf("\tThis is usually to preserve the very first column which often contains\n");
printf("\trow header strings.\n");
printf("\n");
printf("\tThe '-v flip=1' setting will change the order of division, returning\n");
printf("\tcol[n+m/2]/col[n].\n");
printf("\n");
printf("ARGUMENTS\n");
printf("\n");
printf("\tThe following variables passed with -v <var>=<val>\n");
printf("\tprogram behaviour:\n");
printf("\n");
printf("\t-v width=<cellWidth>\n");
printf("\tSet the display cell width to <cellWidth>. Default is 12.\n");
printf("\n");
printf("\t-v precision=<precision>\n");
printf("\tSet the display precision to <precision>. Default is 4.\n");
printf("\n");
printf("\t-v flip=1\n");
printf("\tChange the order of division from the default col1/col2 to col2/col1.\n");
printf("\n");
printf("EXAMPLES\n");
printf("\n");
printf("\tTo process two output files, say 'l1.txt' and 'l2.txt' that contain\n");
printf("\tmatrixes, with first column containing header strings and first\n");
printf("\tcontaining the text string SUBJECT,\n");
printf("\n");
printf(" vcat l1.txt l2.txt | grep -v SUBJECT | col_div2.awk -v start=2 -v preserve=1\n");
printf("\n");
}
BEGIN {
if(!width) width = 12;
if(!precision) precision = 4;
if(!start) start = 1;
if(help) {
synopsis_show();
exit(0);
}
}
#
# Main function
#
{
# Populate each out matrix entry 'o' with 'c[n]/c[n+m/2]'
if(preserve)
printf("%*s", width, $preserve);
for(colIndex = start; colIndex <= NF/2; colIndex++) {
f_first = $colIndex;
f_second = $(colIndex + NF/2);
f_result = f_first / f_second;
if(flip)
f_result = f_second / f_first;
printf("%*.*f", width, precision, f_result);
}
printf("\n");
}
END {
}