forked from BlueWallet/BlueWallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove-loc-key.sh
executable file
·87 lines (78 loc) · 2.07 KB
/
remove-loc-key.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
# Check if a nested key path is provided
if [ -z "$1" ]; then
echo "Usage: $0 nested_key"
echo "Example: $0 autofill_word.error"
exit 1
fi
NESTED_KEY=$1
IFS='.' read -r -a KEYS <<< "$NESTED_KEY"
# Define the potential directories containing JSON files
JSON_DIR1="../loc/"
JSON_DIR2="loc/"
# Check which directory exists and use it
if [ -d "$JSON_DIR1" ]; then
JSON_DIR="$JSON_DIR1"
elif [ -d "$JSON_DIR2" ]; then
JSON_DIR="$JSON_DIR2"
else
echo "Neither $JSON_DIR1 nor $JSON_DIR2 exists. Exiting."
exit 1
fi
# Function to remove the nested key from a JSON file
remove_nested_key_with_awk() {
local file=$1
awk -v keys="${KEYS[*]}" '
BEGIN {
split(keys, k, " ");
key_count = length(k);
skip = 0;
match_count = 0;
brace_count = 0;
delete_line = 0
}
{
if (match_count < key_count && $0 ~ "\""k[match_count+1]"\" *:") {
match_count++;
if (match_count == key_count) {
skip = 1;
delete_line = NR;
if ($0 ~ /{/) {
brace_count = 1;
} else {
brace_count = 0;
}
}
} else if (match_count < key_count && $0 ~ /{/) {
brace_count++;
} else if (match_count < key_count && $0 ~ /}/) {
brace_count--;
if (brace_count == 0) {
match_count = 0;
}
}
if (skip && $0 ~ /{/) {
brace_count++;
}
if (skip && $0 ~ /}/) {
brace_count--;
}
if (skip && brace_count == 0) {
skip = 0;
if (delete_line > 0) {
next;
}
}
if (skip == 0) {
print $0;
}
}' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
}
# Iterate over all JSON files in the chosen directory
for file in "$JSON_DIR"*.json; do
if [ -f "$file" ]; then
echo "Processing $file..."
remove_nested_key_with_awk "$file"
fi
done
echo "Done."