forked from dydxprotocol/v4-abacus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_remove_attr.sh
executable file
·48 lines (40 loc) · 1023 Bytes
/
json_remove_attr.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
#!/bin/bash
# Check if jq is installed
if ! command -v jq &> /dev/null
then
echo "jq is not installed. Please install jq and try again."
exit 1
fi
# Function to display usage information
usage() {
echo "Usage: $0 -f <json_file> -a <attribute>"
echo " -f <json_file> Path to the JSON file"
echo " -a <attribute> Attribute to remove"
exit 1
}
# Parse command-line arguments
while getopts "f:a:" opt; do
case $opt in
f)
JSON_FILE=$OPTARG
;;
a)
ATTRIBUTE=$OPTARG
;;
*)
usage
;;
esac
done
# Check if both JSON file and attribute are provided
if [ -z "$JSON_FILE" ] || [ -z "$ATTRIBUTE" ]; then
usage
fi
# Check if the JSON file exists
if [ ! -f "$JSON_FILE" ]; then
echo "File not found: $JSON_FILE"
exit 1
fi
# Remove the attribute from the JSON file
jq "walk(if type == \"object\" then del(.${ATTRIBUTE}) else . end)" "$JSON_FILE" > tmp.json && mv tmp.json "$JSON_FILE"
echo "Attribute '$ATTRIBUTE' removed from '$JSON_FILE'"