-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhgrep
More file actions
executable file
·46 lines (37 loc) · 1.06 KB
/
hgrep
File metadata and controls
executable file
·46 lines (37 loc) · 1.06 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
#!/bin/bash
#
# hgrep - grep with headers
#
# written by Krishna Roskin, updated by Phillip Dexheimer (pdexheimer@gmail.com), 2019
#
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-?" ]; then
>&2 cat <<EOHelp
hgrep - grep with headers
Displays the first line (the header line) of the file being grepped,
before searching for the pattern. Arguments are the same as grep
User Beware:
1) Any shell aliases for grep are ignored
2) If the input is stdin, the first line will be consumed. This
will cause, eg, hgrep -n to be off by one
3) If multiple input files are supplied, only the first line of
the first will be displayed
EOHelp
exit 1
fi
# Remove any arguments that begin with -
ARGS=${@##-*}
ARGS=($ARGS)
# The input is always the second file (if present)
# If there is no second file, it's stdin
FILE=${ARGS[1]:--}
exec 6<&0 # save stdin into filhandle 6
if [ "$FILE" != "-" ]; then
exec < $FILE # redirect the file to stdin
fi
tmp=$IFS
IFS=$'\n'
read header
echo $header
IFS=$tmp
exec 0<&6 6<&- # Restore stdin and delete filehandle 6
exec grep "$@"