-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlabels_txt_to_json.sh
executable file
·45 lines (36 loc) · 1.04 KB
/
labels_txt_to_json.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
# Script to convert a labels.txt file to a json file
# labels.txt format: <filename> <label>
# json format: {"<filename>": "<label>"}
# Usage: ./labels_txt_to_json.sh <labels.txt> <output.json>
# Check if the number of arguments is correct
if [ $# -ne 2 ]; then
echo "Usage: ./labels_txt_to_json.sh <labels.txt> <output.json>"
exit 1
fi
# Check if the labels.txt file exists
if [ ! -f $1 ]; then
echo "File $1 does not exist"
exit 1
fi
# Check if the output file already exists
if [ -f $2 ]; then
echo "File $2 already exists"
exit 1
fi
# Create the output file
touch $2
# Write the json file
echo "{" >> $2
while read line; do
filename=$(echo $line | cut -d' ' -f1)
# Labels can contain spaces, so we need to get the rest of the line
label=$(echo $line | cut -d' ' -f2-)
# Labels can contain double quotes, so we need to escape them
label=$(echo $label | sed 's/"/\\"/g')
echo " \"$filename\": \"$label\"," >> $2
done < $1
# Remove the last comma
sed -i '$ s/.$//' $2
echo "}" >> $2
echo "Done"
exit 0