h8rt3rmin8r

jsoncombine.sh

Jan 7th, 2019
231
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /bin/bash
  2. ################################################################################
  3. #
  4. #     ___ _____  _____ _   _   _____ ________  _________ _____ _   _  _____
  5. #    |_  /  ___||  _  | \ | | /  __ \  _  |  \/  || ___ \_   _| \ | ||  ___|
  6. #      | \ `--. | | | |  \| | | /  \/ | | | .  . || |_/ / | | |  \| || |__  
  7. #      | |`--. \| | | | . ` | | |   | | | | |\/| || ___ \ | | | . ` ||  __|
  8. #  /\__/ /\__/ /\ \_/ / |\  | | \__/\ \_/ / |  | || |_/ /_| |_| |\  || |___
  9. #  \____/\____/  \___/\_| \_/  \____/\___/\_|  |_/\____/ \___/\_| \_/\____/
  10. #
  11. #
  12. ################################################################################
  13. #
  14. ## INTRODUCTION:
  15. #
  16. #      Script Name: [ jsoncombine.sh ]
  17. #      Source Code: [ http://bit.ly/json-combine ]
  18. #
  19. #      This script combines the contents of a group of JSON formatted data
  20. #      files into a unified file with sections designated by JSON keys which
  21. #      correspond to the file name from which the respective data was found.
  22. #
  23. #      Default execution will operate within the current working directory.
  24. #
  25. ## INSTALLATION:
  26. #
  27. #      Save this script locally as "jsoncombine.sh" and make it executable:
  28. #         sudo chmod +x jsoncombine.sh
  29. #
  30. #      Run the script in the following manner:
  31. #         ./jsoncombine.sh <TARGET_DIRECTORY>
  32. #
  33. #      Enable this script to have global execution availability by placing it
  34. #      into /usr/local/bin (or somewhere else in your user's PATH). By doing
  35. #      this, you can call "jsoncombine.sh" from anywhere on the system.
  36. #
  37. ## USAGE:
  38. #
  39. #      jsoncombine [OPTIONS]
  40. #      
  41. ## OPTIONS:
  42. #
  43. #      --help        |  Print this help text
  44. #      --silent      |  Execute operations without any output to stdout
  45. #      <DIRECTORY>   |  Operate on directory other than the working directory
  46. #
  47. ## LICENSE:
  48. #
  49. #      This script was created by h8rt3rmin8r on 20190108 for ResoNova.
  50. #
  51. #      Copyright 2018 ResoNova International Consulting, LLC
  52. #
  53. #      Licensed under the Apache License, Version 2.0 (the "License");
  54. #      you may not use this file except in compliance with the License.
  55. #      You may obtain a copy of the License at
  56. #
  57. #          http://www.apache.org/licenses/LICENSE-2.0
  58. #
  59. #      Unless required by applicable law or agreed to in writing, software
  60. #      distributed under the License is distributed on an "AS IS" BASIS,
  61. #      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  62. #      See the License for the specific language governing permissions and
  63. #      limitations under the License.
  64. #
  65. ################################################################################
  66.  
  67. # VARIABLE DECLARATIONS
  68.  
  69. # Self awareness variables
  70. HERE=$(pwd)
  71. SELF="${0##*/}"
  72. SELF_OPEN="[${SELF}]"
  73. DEPENDS=( "jq" )
  74. DEPENDS_CHK=$(jq --version &>/dev/null; echo $?)
  75. # Operations variables
  76. TARGET_DIR="${HERE}"
  77. FILES=( $(ls ${TARGET_DIR}) )
  78. VALID_FILES=( $( for i in ${FILES[@]}; do if [[ $i =~ .json$ ]]; then echo $i; fi; done ) )
  79. VALID_FILES_NUM=$(echo "${#VALID_FILES[@]}")
  80. # Verbosity variables
  81. OBX='/dev/stdout'
  82. VB_E1="${SELF_OPEN} ERROR: Required package 'jq' is not installed."
  83. VB_E2="              You can install it with 'sudo apt-get install jq'"
  84. VB_E3_PART="${SELF_OPEN} ERROR: Only one '.json' file type was found."
  85. VB_E3="${VB_E3_PART} There is a minimum of TWO such file types."
  86. VB_1="${SELF_OPEN} Processing item: "
  87.  
  88. # FUNCTION DECLARATIONS
  89.  
  90. function depends_check() {
  91.     if [[ "${DEPENDS_CHK}" -gt 0 ]];
  92.     then
  93.         echo "${VB_E1}" &>${OBX}
  94.         echo "${VB_E2}" &>${OBX}
  95.         exit 1
  96.     fi
  97. }
  98.  
  99. function opps_test() {
  100.     # Verify the presence of at least TWO .json files
  101.     if [[ ${VALID_FILES_NUM} -lt 2 ]];
  102.     then
  103.         echo "${VB_E3}" &>${OBX}
  104.         exit 1
  105.     else
  106.         # Assign valid file limits after validation to avoid errors to stdout
  107.         export VF_FIRST_BASE="${VALID_FILES[0]}"
  108.         export VF_LAST_BASE="${VALID_FILES[-1]}"
  109.         export VF_FIRST="${VF_FIRST_BASE%.json}"
  110.         export VF_LAST="${VF_LAST_BASE%.json}"
  111.         export OUTPUT="${VF_FIRST}_${VF_LAST}.json"
  112.     fi
  113.     return
  114. }
  115.  
  116. function return_item_name() {
  117.     echo "${ITEM_NAME%.json}"
  118.     return
  119. }
  120.  
  121. function main_process() {
  122.     # Main operational function
  123.     opps_test
  124.     touch ${OUTPUT}
  125.     echo '{' > ${OUTPUT}
  126.     TOP_LIMIT="${VALID_FILES_NUM}"
  127.     for i in ${VALID_FILES[@]};
  128.     do
  129.         # Check how many files remain and assign the last snippet accordingly
  130.         if [[ "${TOP_LIMIT}" -gt 1 ]];
  131.         then
  132.             END_SEGMENT='],'
  133.         else
  134.             END_SEGMENT=']}'
  135.         fi
  136.         # Execute the loop with the current file data from the valid files list
  137.         echo "${VB_1} $i" &>${OBX}
  138.         export ITEM_NAME="$i"
  139.         echo '"'$(return_item_name)'": [' >> ${OUTPUT}
  140.         cat "$i" >> ${OUTPUT}
  141.         echo ${END_SEGMENT} >> ${OUTPUT}
  142.         # Update the limit counter and zero out the "ITEM_NAME" variable
  143.         let TOP_LIMIT=TOP_LIMIT-1
  144.         export ITEM_NAME=''
  145.     done
  146.     # Beautify the output file by passing it through "jq"
  147.     touch ".temp.json"
  148.     cat ${OUTPUT} | jq '.' > ".temp.json"
  149.     mv ".temp.json" ${OUTPUT}
  150.     return
  151. }
  152.  
  153. function final_output() {
  154.     # Final output to the terminal before script is killed
  155.     echo "[${SELF}] Done." &>${OBX}
  156.     echo "[${SELF}] JSON Combination Output:  ${OUTPUT}" &>${OBX}
  157.     exit 0
  158. }
  159.  
  160. function test_function() {
  161.     echo "" &>${OBX}
  162.     echo " HERE:                  ${HERE}" &>${OBX}
  163.     echo " SELF:                  ${SELF}" &>${OBX}
  164.     echo " TARGET_DIR:            ${TARGET_DIR}" &>${OBX}
  165.     echo " VALID_FILES_NUM:       ${VALID_FILES_NUM}" &>${OBX}
  166.     echo "" &>${OBX}
  167.     exit 0
  168. }
  169.  
  170. function help_text() {
  171.     # Output the script help text
  172.     clear
  173.     cat "$0" | head -n 63 | tail -n +2 | tr -d '#'
  174.     echo ""
  175.     exit 0
  176. }
  177.  
  178. # VALIDATION AND OPERATIONS EXECUTION
  179.  
  180. if [[ "$@" =~ "-h" || "$@" =~ "-help" || "$@" =~ "--help" ]];
  181. then
  182.     # Help text filter
  183.     help_text
  184. fi
  185.  
  186. # Initial dependancy check
  187. depends_check
  188.  
  189. # Operational filters
  190. if [[ "$@" =~ "-s" || "$@" =~ "--silent" || "$@" =~ "-silent" ]];
  191. then
  192.     OBX="/dev/null"
  193. fi
  194.  
  195. if [[ "x$1" == "x" ]];
  196. then
  197.     main_process
  198.     final_output
  199. else
  200.     if [[ "$@" =~ "--test" || "$@" =~ "-test" || "$@" =~ "-t" ]];
  201.     then
  202.         # Allow for a test-run to see what the variables are being set to
  203.         test_function
  204.     fi
  205.     if [[ -d "$1" ]];
  206.     then
  207.         # If the user wants to use a different directory (other than the current one)
  208.         # the core variables must be re-declared to reflect accordingly:
  209.  
  210.         # Unset arrays first
  211.         unset FILES
  212.         unset VALID_FILES
  213.         # Reset arrays with altered values
  214.         TARGET_DIR="$1"
  215.         FILES=( $(ls ${TARGET_DIR}) )
  216.         VALID_FILES=( $( for i in ${FILES[@]}; do if [[ $i =~ .json$ ]]; then echo $i; fi; done ) )
  217.         VALID_FILES_NUM=$(echo "${#VALID_FILES[@]}")
  218.         # Execute operations
  219.         main_process
  220.         final_output
  221.     fi
  222. fi
  223.  
  224. ################################################################################
  225.                                                    #                           #
  226.                                                    #  "think outside the box"  #
  227.                                                    #                           #
  228.                                                    #    ($) ¯\_(ツ)_/¯ (฿)     #
  229.                                                    #                           #
  230.                                                    #############################
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×