-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline-range.sh
More file actions
68 lines (59 loc) · 1.57 KB
/
line-range.sh
File metadata and controls
68 lines (59 loc) · 1.57 KB
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
#!/bin/sh
usage() {
echo "Usage: $(basename "$0") LINERANGE [FILE]
LINERANGE Line range to print in the form \`X:Y\`.
1-based, both inclusive.
Omitting \`X\` implicitly sets it to 1.
Omitting \`Y\` implicitly sets it to (line_count+1).
-h, --help Print this message and exit
" 1>&2
exit
}
die() {
{
echo "$@"
echo ""
echo "See -h for help"
} 1>&2
exit 1
}
echo " $* " | grep -qE "( -h )|( --help )" && usage
line_range=$1
fname=$2
[ -z "$line_range" ] && die "line range required"
sed_range=$(echo "$line_range" | awk -F: '
{ line_count = NR
if ($1) {
if($1 ~ /^[-+]?[0-9]+$/)
from = +$1
else
from = 0
} else
from = 1
if ($2){
if($2 ~ /^[-+]?[0-9]+$/)
to = +$2
else
to = 0
} else
to = "$"
}
END { err_start = "Line range error: "
if (line_count != 1) {
print err_start "Unexpected new line"
exit(1)
}
if ((to != "$" && (to < 1 || int(to) != to)) || (from < 1 || int(from) != from)) {
print err_start "Expected line range of the form [X]:[Y] where X and Y are integers >=1"
exit(1)
}
if(to != "$" && to < from) {
print err_start "Start range should be smaller than or equal to end range"
exit(1)
}
print from "," to "p" }') || die "$sed_range"
if [ -z "$fname" ]; then
exec sed -n "$sed_range"
else
exec sed -n "$sed_range" "$fname"
fi