forked from douglasgscofield/tinyutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boolify
executable file
·52 lines (48 loc) · 1.42 KB
/
boolify
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
#!/usr/bin/awk -f
#
# Copyright (c) 2012,2014 Douglas G. Scofield, Uppsala University
#
# This code is covered by the Gnu GPLv2, please see LICENSE.
# Bugs and suggestions to https://github.com/douglasgscofield/tinyutils/issues.
#
# Turns a column into a boolean value (0 or 1)
#
# CHANGELOG
# 2014-05-27 : don't quit on missing-column error
# 2013-05-15 : make FS and OFS be "\t" by default
# 2012-12-02 : clean the script up a bit
# 2012-11-27 : create the script
BEGIN {
# parameters
FS = "\t";
OFS = "\t";
header = 0; # does the input have a header? if so how many lines?
skip_comment = 1; # skip '#' lines on input
false = 0; # set to the value that should be false
# min # set min if you want the script to assume input values
# are numeric, it will only make values true that
# are >= min.
col = 1; # which column do we boolify?
}
{
if (NR <= header) { print; next; }
if (skip_comment && $0 ~ /^#/) { print; next; }
if (NF < col) {
print "on input line " NR " missing requested column" > "/dev/stderr";
next;
}
if (min) {
if ($(col) >= min) {
$(col) = 1;
} else {
$(col) = 0;
}
} else if ($(col) != false) {
$(col) = 1;
} else {
$(col) = 0;
}
print;
}