-
Notifications
You must be signed in to change notification settings - Fork 0
/
,header
executable file
·93 lines (81 loc) · 1.6 KB
/
,header
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
#!/bin/bash
# header: add, replace, and delete header lines.
#
# Example usage:
# $ seq 10 | header -a 'values'
# $ seq 10 | header -a 'VALUES' | header -e 'tr "[:upper:]" "[:lower:]"'
# $ seq 10 | header -a 'values' | header -d
# $ seq 10 | header -a 'multi\nline' | header -n 2 -e "paste -sd_"
#
# See also: body (https://github.com/jeroenjanssens/command-line-tools-for-data-science)
#
# Author: http://jeroenjanssens.com
usage () {
cat << EOF
header: add, replace, and delete header lines.
usage: header OPTIONS
OPTIONS:
-n Number of lines to consider as header [default: 1]
-a Add header
-r Replace header
-e Apply expression to header
-d Delete header
-h Show this message
Example usage:
$ seq 10 | header -a 'values'
$ seq 10 | header -a 'VALUES' | header -e 'tr "[:upper:]" "[:lower:]"'
$ seq 10 | header -a 'values' | header -d
$ seq 10 | header -a 'multi\nline' | header -n 2 -e "paste -sd_"
See also: body
EOF
}
get_header () {
for i in $(seq $NUMROWS); do
IFS= read -r LINE
OLDHEADER="${OLDHEADER}${LINE}\n"
done
}
print_header () {
echo -ne "$1"
}
print_body () {
cat
}
OLDHEADER=
NUMROWS=1
while getopts "dn:ha:r:e:" OPTION
do
case $OPTION in
n)
NUMROWS=$OPTARG
;;
a)
print_header "$OPTARG\n"
print_body
exit 0
;;
d)
get_header
print_body
exit 0
;;
r)
get_header
print_header "$OPTARG\n"
print_body
exit 0
;;
e)
get_header
print_header "$(echo -ne $OLDHEADER | eval $OPTARG)\n"
print_body
exit 0
;;
h)
usage
exit 0
;;
esac
done
get_header
print_header "${OLDHEADER}"