dzocesrce

[OS] .json --> .csv

Jan 21st, 2024
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.86 KB | None | 0 0
  1. #!/bin/bash
  2. #if the number of arguments is not 2
  3. if [ $# -ne 2 ]
  4. then
  5.     echo "USAGE: `basename $0` folder_from folder_to"
  6.     exit 1
  7. fi
  8. #if an input.json file does not exist in source folder
  9. if [ ! -f $1/input.json ]
  10. then
  11.     echo "Vleznata datoteka ne postoi"
  12.     exit 1
  13. fi
  14. #if the destination folder does not exist
  15. if [ ! -d $2 ]
  16. then
  17.     mkdir $2
  18. fi
  19. FROM=$1
  20. TO=$2
  21. #create our final output.csv file and a temp.txt file for input info manipulation
  22. touch output.csv
  23. touch temp.txt
  24. #include only the lines that consist double dot(info lines), then change global empty spacez, comaz and quotation markz with nothing
  25. echo `cat $FROM/input.json | grep : | sed -e 's/ //g' -e 's/,//g' -e 's/"//g'` >> temp.txt
  26. #get sum of all durations
  27. sum_duration=`cat temp.txt | grep ^duration | awk -F: 'BEGIN {total=0} {total+=$2} END {print total}'`
  28. #get number of lines that contain duration attribute
  29. number_of_audios=`cat temp.txt | grep ^duration | wc -l`
  30. #get average duration of an audio file
  31. average_duration=`expr $sum_duration / $number_of_audios`
  32. #instead of working with rows of one file, make a field with elements of each attribute
  33. FILEPATHS=`cat temp.txt | grep ^filepath | awk -F: 'print $2'`
  34. DURATIONS=`cat temp.txt | grep ^duration | awk -F: 'print $2'`
  35. FILESIZES=`cat temp.txt | grep ^filesize | awk -F: 'print $2'`
  36. count=0
  37. echo "id,filepath,filesize,is_longer" >> output.csv
  38. #do as many iterations of while as there are audio files
  39. while [ count -lt $number_of_audios ]
  40. do
  41.     is_longer=0
  42.     if [ ${DURATIONS[${count} -gt $average_duration ]
  43.     then
  44.         is_longer=1
  45.     fi
  46.     id_number=`expr $count + 1`
  47.     echo "${id_number},${FILEPATHS[${count}]},${FILESIZES[${count}]},${is_longer}" >> output.csv
  48.     count=`expr $count + 1`
  49. done
  50. #move the fucking file of interest
  51. mv output.csv $TO/output.csv
  52. cat $TO/output.csv
  53. #FINALLY ITS OVER
  54.  
Add Comment
Please, Sign In to add comment