ghallas

extract field from json

Sep 23rd, 2021
833
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. if [ $# -ne 2 ] && [ $# -ne 3 ]
  3.   then
  4.     echo "use with arguments: ./exjf.sh <file to parse> <key to extract> <optional:-no_quotes> if the value us not enclosed in quotes" 
  5.     exit 0;
  6. fi
  7.  
  8. REMOVE_QUOTES=true;
  9. if [ "$3" == "-no_quotes" ];
  10.   then
  11.     REMOVE_QUOTES=false;
  12. fi 
  13.  
  14. OUT_FILE="${1}.out";
  15. echo "key: $1";
  16. echo "Filename: $2";
  17. echo "Remove quotes: $REMOVE_QUOTES";
  18. grep "$1" $2 > $OUT_FILE;
  19. if $REMOVE_QUOTES
  20.   then
  21.     sed -i "s/\"$1\": \"//g" $OUT_FILE;
  22.     sed -i 's/\",//g' $OUT_FILE;
  23.   else
  24.     sed -i "s/\"$1\": //g" $OUT_FILE;
  25.     sed -i 's/,//g' $OUT_FILE;
  26. fi
  27.  
  28.  
RAW Paste Data