Advertisement
darsinqauros

Simple CSV2JSON

Apr 3rd, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.69 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #cat alunos.txt
  4. #Nome;turma;periodo
  5. #Zé das cove;a2;matutino
  6. #Tião;d3;vespertino
  7. #Mariazinha;w1;matutino
  8. #Leo;k9;noturno
  9.  
  10.  
  11. jsonObjPatern="{"$(head -n 1 alunos.txt | sed  's/[;]/,/g')"}"
  12.  
  13. IFS=";" read -ra fields <<< "$(head -n 1 alunos.txt)"
  14.  
  15. numberOfElements=${#fields[@]}
  16.  
  17. while read csvLine; do
  18.  
  19.     IFS=";" read -ra values <<< $(echo $csvLine)
  20.  
  21.     asdfgh=0
  22.  
  23.     obj=$jsonObjPatern
  24.  
  25.     while [ ${asdfgh:-0} -lt ${numberOfElements:-0} ]; do
  26.  
  27.         qwerty=${fields[$asdfgh]}
  28.  
  29.         zxcvbn=${fields[$asdfgh]}":"${values[$asdfgh]}
  30.  
  31.         obj=${obj/$qwerty/$zxcvbn}
  32.  
  33.         asdfgh=$(($asdfgh+1))
  34.  
  35.     done
  36.  
  37.     jsonObj=$jsonObj$obj","
  38.  
  39. done <<< $(cat alunos.txt | sed -n '1!p')
  40.  
  41. echo $jsonObj
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement