-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_lines_w_pattern.sh
executable file
·57 lines (47 loc) · 1.26 KB
/
get_lines_w_pattern.sh
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
#!/bin/bash
# Setting to strict mode
set -euo pipefail
IFS=$'\n\t'
#### FUNCTIONS
usage()
{
echo "usage: get_lines_w_pattern.sh [[[-f file ] [-c column] [-p pattern] [-o outname]] | [-h]]"
}
#### MAIN
# Test whether all required inputs are present
if [[ $1 == -h ]] || [[ $# != 8 ]]
then
usage
exit
fi
# Get parameters
while [ $# -gt 0 ]; do
case $1 in
-f | --file ) shift
filename=$1
;;
-o | --out) shift
outname=$1
;;
-c | --column ) shift
column=$1
;;
-p | --pattern ) shift
pattern=$1
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
printf "\nAll lines with pattern \"%s\" in column %i are retrieved from %s and written to %s.\n" \
${pattern}\
${column}\
${filename}\
${outname}
# Get lines with matching pattern in requested column
zcat ${filename} | awk -v col="${column}" -v pat="${pattern}" '$col == pat' > ${outname}
printf "\nDONE!\n"