-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinrange
executable file
·68 lines (62 loc) · 2.09 KB
/
inrange
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
#!/usr/bin/awk -f
#
# Copyright (c) 2012,2015 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.
#
# Only pass along data lines for which column values fall within the specified range,
# except with inverse=1
#
# CHANGELOG
# 2017-08-10 : add inverse option
# 2015-03-31 : fix bug with abs=: constrain option processing to first input line
# 2014-05-27 : don't quit on missing-column error
# 2013-05-15 : make FS and OFS be "\t" by default
# 2012-12-04 : create the script
BEGIN {
# parameters
FS = "\t";
OFS = "\t";
abs = "undef";
min = "undef";
max = "undef";
header = 0; # does the input have a header? if so how many lines?
skip_comment = 1; # skip '#' lines on input
skip_missing = 1; # skip lines without the column
col = 1; # which column do we operate on?
inverse = 0; # invert the sense of the test: all but the lines that are in range
}
{
if (NR == 1) {
if (abs != "undef" && min == "undef" && max == "undef") {
if (abs < 0) {
print "abs= must be >= 0";
exit 1;
}
max = abs; min = -abs; abs = "undef";
} else if ((abs != "undef" && (min != "undef" || max != "undef")) ||
(abs == "undef" && min == "undef" && max == "undef")) {
print "specify min=, max=, both min= and max=, or abs= (absolute value of range) on the command line";
exit 1;
}
}
if (NR <= header) { print; next; }
if (skip_comment && $0 ~ /^#/) { print; next; }
if (NF < col) {
print "on input line " NR " missing requested column" > "/dev/stderr";
if (skip_missing) {
next;
} else {
print;
next;
}
}
if ((min == "undef" || $(col) >= min) && (max == "undef" || $(col) <= max)) {
if (inverse == 0)
print;
} else if (inverse == 1)
print;
}