-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnthline
executable file
·52 lines (47 loc) · 979 Bytes
/
nthline
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
#! /bin/bash
#trap для восстановления cnorm
usage(){
echo nthline - read text and print nth line with prefix >&2
echo usage: nthline [-n N] [-p prefix] [-1] [-c color] >&2
echo option -1 mean that every string will be truncated and overprinted on the same line by color ; it is slow >&2
echo defaults: N=100 prefix=\"\" >&2
exit $1
}
N=100
prefix=""
oneline=0
color=-1
while getopts n:p:h1c: opts ; do
case $opts in
n) N=$OPTARG
;;
p) prefix="$OPTARG"
;;
1) oneline=1
;;
h) usage 0
;;
c) color="$OPTARG"
;;
*) usage 1
;;
esac
done
[ $oneline == 1 ] && tput civis
cols=$(( $(tput cols) - 2))
[ $color != -1 ] && color_o=$(tput setf $color) || color_o=
I=0
while read line; do
if [ $oneline == 1 ] ; then
tput sc
echo -n "$color_o${line:0:$cols}"
tput el
tput rc
fi
I=$(($I + 1))
if [ $I == $N ]; then
echo "$prefix $line"
I=0
fi
done
[ $oneline == 1 ] && echo $(tput cnorm)