-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathline
executable file
·94 lines (88 loc) · 3.27 KB
/
line
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
#!/usr/bin/awk -f
#
# Copyright (c) 2016 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 through the requested lines. The parameters are applied via
# logical 'or', so a line will be printed if it matches any of line=, or
# min=/max=, or stride=/chunk=/first=. These all may be specified or just one.
#
# CHANGELOG
# 2017-10-13 : add around=
# 2017-08-10 : add inverse= option
# 2016-08-15 : start stride mode with stride=, first=, chunk=
# stride= number of lines in stride; stride=4 is every fourth line
# first= line to begin strides
# chunk= number of lines to take per stride
# 2016-07-27 : create the script
BEGIN {
# parameters
line = 0; # a single line to show; always applies if supplied
around = 0; # number of lines before and after line=
min = 0; # starting line of a line range
max = 0; # ending line of a line range
stride = 0; # stride size
first = 1; # first line of stride
chunk = 1; # number of lines to take per stride
inverse = 0; # invert the sense of the filter, print lines that do not fit
# internal variables
_print_line = 0; # print the current line?
_next_first = 0; # first line of the next chunk
_in_chunk = 0; # within a chunk?
}
{
if (NR == 1) {
if (!line && !min && !max && !stride) {
print "no line(s) specified" > "/dev/stderr";
exit 1;
} else if (line < 0 || around < 0 || min < 0 || max < 0 || stride < 0) {
print "line specifiers must be 0 or positive" > "/dev/stderr";
exit 1;
} else if (max && min > max) {
print "if max specified, max must be >= min" > "/dev/stderr";
exit 1;
} else if (min && max && min > max) {
print "if min and max specified, min must be <= max" > "/dev/stderr";
exit 1;
} else if (stride && (first < 1 || chunk < 1)) {
print "stride specifiers first= and chunk= must be at least 1" > "/dev/stderr";
exit 1;
} else if (stride && chunk > stride) {
print "chunk size must be <= stride" > "/dev/stderr";
exit 1;
}
if (stride)
_next_first = first; # prime the stride
}
_print_line = 0;
if (line && NR >= line - around && NR <= line + around) {
_print_line = 1;
} else if (min && NR >= min) {
if (!max || (max && NR <= max))
_print_line = 1;
} else if (max && NR <= max) {
if (!min || (min && NR >= min))
_print_line = 1;
} else if (stride && _in_chunk) {
_print_line = 1;
_in_chunk--;
} else if (stride && NR == _next_first) {
_print_line = 1;
_in_chunk = chunk - 1;
_next_first = NR + stride;
} else if (!stride &&
(((line && NR > line + around) && (!max || (max && NR > max))) ||
(!line && max && NR > max))) {
# no more lines to show
exit 0;
}
if (_print_line) {
if (inverse == 0)
print;
} else if (inverse == 1)
print;
}