-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanonymize-columns.sh
executable file
·66 lines (52 loc) · 1.56 KB
/
anonymize-columns.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
58
59
60
61
62
63
64
65
#!/usr/bin/env bash
export DESCRIPTION="Replaces specific columns in a csv table with generic data in the same format
by mapping '0-9' -> '0', 'a-z' -> 'a' and 'A-Z' -> 'A'"
export USAGE="anonymize-columns [OPTIONS] columns file
columns:
A comma separated list of column ids to anonymize
file:
The csv file to anonymize
Options:
-d, --delimiter <delimiter> Delimiter to split table by
-s, --skip-header Ignore (Don't change) first row table"
set -e
### ARGUMENT PARSING ###
. "$(dirname "$BASH_SOURCE")/lib/parse_args.sh"
declare -a KEYWORDS=("--delimiter" "-d" "-s;bool" "--skip-header;bool")
declare -a REQUIRED=("columns" "file")
set_trap 1 2
parse_args __DESCRIPTION "$DESCRIPTION" __USAGE "$USAGE" "$@"
skip_header="${KW_ARGS["-s"]-false}"
skip_header="${KW_ARGS["--skip-header"]-${skip_header}}"
IFS=',' read -r -a columns <<<"${NAMED_ARGS["columns"]}"
echo "columns: ${columns[@]}"
file="${NAMED_ARGS["file"]}"
delim="${KW_ARGS["-d"]-"|"}"
delim="${KW_ARGS["--delimiter"]-"$delim"}"
######
exec 5< "$file"
while read line <&5
do
if [ "$skip_header" == "true" ]
then
echo "$line"
skip_header=false
continue
fi
for column in ${columns[@]}
do
line="$(
echo "$line" \
| awk -F "$delim" '{
OFS="'$delim'";
{
gsub(/[0-9]/, "0", $'$column')
gsub(/[a-z]/,"a", $'$column')
gsub(/[A-Z]/,"A", $'$column')
}
print
}'
)"
done
echo "$line"
done