-
-
Notifications
You must be signed in to change notification settings - Fork 215
Expand file tree
/
Copy pathremove_string_key_all.sh
More file actions
60 lines (51 loc) · 1.27 KB
/
remove_string_key_all.sh
File metadata and controls
60 lines (51 loc) · 1.27 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash
# Usage:
# ./remove_string_key_all.sh key1 [key2 ...]
# ./remove_string_key_all.sh # interactive mode
set -euo pipefail
RES_DIR="./app/src/main/res"
collect_keys() {
grep -RhoE 'name="[^"]+"' "$RES_DIR"/values*/strings*.xml \
| sed -E 's/name="([^"]+)"/\1/' \
| sort -u
}
read -r -a KEYS <<< "${*:-}"
if [[ ${#KEYS[@]} -eq 0 ]]; then
echo "No key passed. Enter keys manually (space-separated)."
echo
echo "Available keys (first 120):"
collect_keys | head -120
echo
read -r -p "Keys to remove: " MANUAL_KEYS
if [[ -z "$MANUAL_KEYS" ]]; then
echo "No keys entered. Exit."
exit 0
fi
read -r -a KEYS <<< "$MANUAL_KEYS"
fi
FILES=$(find "$RES_DIR" -type f -path '*/values*/strings*.xml' | sort)
if [[ -z "$FILES" ]]; then
echo "No strings*.xml files found under $RES_DIR"
exit 1
fi
for KEY in "${KEYS[@]}"; do
echo "Removing key: $KEY"
for FILE in $FILES; do
awk -v key="$KEY" '
BEGIN { skip = 0 }
{
if ($0 ~ "<string[[:space:]]+name=\"" key "\"[^>]*>") {
skip = 1
}
if (skip) {
if ($0 ~ /<\/string>/) {
skip = 0
}
next
}
print
}
' "$FILE" > "${FILE}.tmp" && mv "${FILE}.tmp" "$FILE"
done
done
echo "Done. Removed ${#KEYS[@]} key(s)."