-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftdmp-filter-table
executable file
·100 lines (73 loc) · 2.02 KB
/
ftdmp-filter-table
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
93
94
95
96
97
98
99
#!/bin/bash
function print_help_and_exit
{
cat >&2 << 'EOF'
'ftdmp-filter-table' filters table by checking if any column value satisfies the provided condition
Options:
space-separated table
Standard output:
space-separated table
Example:
ftdmp-filter-table '<100' colname1 colname2 colname3
ftdmp-filter-table '<100&&' colname1 colname2 colname3
EOF
exit 1
}
if [ -z "$1" ]
then
print_help_and_exit
fi
readonly TMPLDIR=$(mktemp -d)
trap "rm -r $TMPLDIR" EXIT
LOGICOP="||"
COMPARISON="$1"
shift
COMPARISON_POSTFIX="$(echo ${COMPARISON} | sed 's/^.*\(..\)$/\1/')"
if [ "$COMPARISON_POSTFIX" == "||" ] || [ "$COMPARISON_POSTFIX" == "&&" ]
then
LOGICOP="$COMPARISON_POSTFIX"
COMPARISON="$(echo ${COMPARISON} | sed 's/^\(.*\)\(..\)$/\1/')"
fi
if [ -z "$COMPARISON" ]
then
echo >&2 "Error: no comparison operation provided"
exit 1
fi
while [[ $# > 0 ]]
do
COLNAME="$1"
shift
if [ -z "$COLNAME" ]
then
echo >&2 "Error: empty column name in arguments"
exit 1
fi
echo "$COLNAME" >> "$TMPLDIR/input_list"
done
INCOUNT="$(cat "$TMPLDIR/input_list" | sort | uniq | wc -l)"
if [ "$INCOUNT" -lt "1" ]
then
echo >&2 "Error: no input column names provided"
exit 1
fi
cat > "$TMPLDIR/input_table"
head -1 "$TMPLDIR/input_table" | sed 's/\s\+/\n/g' | egrep . > "$TMPLDIR/input_table_columns"
MATCHEDCOUNT="$(cat "$TMPLDIR/input_table_columns" | awk '{print ";" $1 ";"}' | grep -f <(cat "$TMPLDIR/input_list" | awk '{print ";" $1 ";"}') | sort | uniq | wc -l)"
if [ "$INCOUNT" != "$MATCHEDCOUNT" ]
then
echo >&2 "Error: provided column names do not match the input table column names"
exit 1
fi
{
echo 'NR==1 {for(i=1;i<=NF;i++){f[$i]=i} print}'
echo 'NR>1 {if('
cat "$TMPLDIR/input_list" | sed 's/^/\$(f[\"/' | sed 's/$/\"])/' | awk -v comparison="$COMPARISON" -v logicop="$LOGICOP" '{print $0 comparison " " logicop}'
echo ")"
echo '{print $0}}'
} \
| tr '\n' ' ' \
| sed 's/\s||\s*)/ )/' \
| sed 's/\s&&\s*)/ )/' \
| sed 's/$/\n/' \
> "$TMPLDIR/awk_script"
cat "$TMPLDIR/input_table" | awk -f "$TMPLDIR/awk_script"