Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /bin/bash
- ################################################################################
- #
- # ___ _____ _____ _ _ _____ ________ _________ _____ _ _ _____
- # |_ / ___|| _ | \ | | / __ \ _ | \/ || ___ \_ _| \ | || ___|
- # | \ `--. | | | | \| | | / \/ | | | . . || |_/ / | | | \| || |__
- # | |`--. \| | | | . ` | | | | | | | |\/| || ___ \ | | | . ` || __|
- # /\__/ /\__/ /\ \_/ / |\ | | \__/\ \_/ / | | || |_/ /_| |_| |\ || |___
- # \____/\____/ \___/\_| \_/ \____/\___/\_| |_/\____/ \___/\_| \_/\____/
- #
- #
- ################################################################################
- #
- ## INTRODUCTION:
- #
- # Script Name: [ jsoncombine.sh ]
- # Source Code: [ http://bit.ly/json-combine ]
- #
- # This script combines the contents of a group of JSON formatted data
- # files into a unified file with sections designated by JSON keys which
- # correspond to the file name from which the respective data was found.
- #
- # Default execution will operate within the current working directory.
- #
- ## INSTALLATION:
- #
- # Save this script locally as "jsoncombine.sh" and make it executable:
- # sudo chmod +x jsoncombine.sh
- #
- # Run the script in the following manner:
- # ./jsoncombine.sh <TARGET_DIRECTORY>
- #
- # Enable this script to have global execution availability by placing it
- # into /usr/local/bin (or somewhere else in your user's PATH). By doing
- # this, you can call "jsoncombine.sh" from anywhere on the system.
- #
- ## USAGE:
- #
- # jsoncombine [OPTIONS]
- #
- ## OPTIONS:
- #
- # --help | Print this help text
- # --silent | Execute operations without any output to stdout
- # <DIRECTORY> | Operate on directory other than the working directory
- #
- ## LICENSE:
- #
- # This script was created by h8rt3rmin8r on 20190108 for ResoNova.
- #
- # Copyright 2018 ResoNova International Consulting, LLC
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- #
- ################################################################################
- # VARIABLE DECLARATIONS
- # Self awareness variables
- HERE=$(pwd)
- SELF="${0##*/}"
- SELF_OPEN="[${SELF}]"
- DEPENDS=( "jq" )
- DEPENDS_CHK=$(jq --version &>/dev/null; echo $?)
- # Operations variables
- TARGET_DIR="${HERE}"
- FILES=( $(ls ${TARGET_DIR}) )
- VALID_FILES=( $( for i in ${FILES[@]}; do if [[ $i =~ .json$ ]]; then echo $i; fi; done ) )
- VALID_FILES_NUM=$(echo "${#VALID_FILES[@]}")
- # Verbosity variables
- OBX='/dev/stdout'
- VB_E1="${SELF_OPEN} ERROR: Required package 'jq' is not installed."
- VB_E2=" You can install it with 'sudo apt-get install jq'"
- VB_E3_PART="${SELF_OPEN} ERROR: Only one '.json' file type was found."
- VB_E3="${VB_E3_PART} There is a minimum of TWO such file types."
- VB_1="${SELF_OPEN} Processing item: "
- # FUNCTION DECLARATIONS
- function depends_check() {
- if [[ "${DEPENDS_CHK}" -gt 0 ]];
- then
- echo "${VB_E1}" &>${OBX}
- echo "${VB_E2}" &>${OBX}
- exit 1
- fi
- }
- function opps_test() {
- # Verify the presence of at least TWO .json files
- if [[ ${VALID_FILES_NUM} -lt 2 ]];
- then
- echo "${VB_E3}" &>${OBX}
- exit 1
- else
- # Assign valid file limits after validation to avoid errors to stdout
- export VF_FIRST_BASE="${VALID_FILES[0]}"
- export VF_LAST_BASE="${VALID_FILES[-1]}"
- export VF_FIRST="${VF_FIRST_BASE%.json}"
- export VF_LAST="${VF_LAST_BASE%.json}"
- export OUTPUT="${VF_FIRST}_${VF_LAST}.json"
- fi
- return
- }
- function return_item_name() {
- echo "${ITEM_NAME%.json}"
- return
- }
- function main_process() {
- # Main operational function
- opps_test
- touch ${OUTPUT}
- echo '{' > ${OUTPUT}
- TOP_LIMIT="${VALID_FILES_NUM}"
- for i in ${VALID_FILES[@]};
- do
- # Check how many files remain and assign the last snippet accordingly
- if [[ "${TOP_LIMIT}" -gt 1 ]];
- then
- END_SEGMENT='],'
- else
- END_SEGMENT=']}'
- fi
- # Execute the loop with the current file data from the valid files list
- echo "${VB_1} $i" &>${OBX}
- export ITEM_NAME="$i"
- echo '"'$(return_item_name)'": [' >> ${OUTPUT}
- cat "$i" >> ${OUTPUT}
- echo ${END_SEGMENT} >> ${OUTPUT}
- # Update the limit counter and zero out the "ITEM_NAME" variable
- let TOP_LIMIT=TOP_LIMIT-1
- export ITEM_NAME=''
- done
- # Beautify the output file by passing it through "jq"
- touch ".temp.json"
- cat ${OUTPUT} | jq '.' > ".temp.json"
- mv ".temp.json" ${OUTPUT}
- return
- }
- function final_output() {
- # Final output to the terminal before script is killed
- echo "[${SELF}] Done." &>${OBX}
- echo "[${SELF}] JSON Combination Output: ${OUTPUT}" &>${OBX}
- exit 0
- }
- function test_function() {
- echo "" &>${OBX}
- echo " HERE: ${HERE}" &>${OBX}
- echo " SELF: ${SELF}" &>${OBX}
- echo " TARGET_DIR: ${TARGET_DIR}" &>${OBX}
- echo " VALID_FILES_NUM: ${VALID_FILES_NUM}" &>${OBX}
- echo "" &>${OBX}
- exit 0
- }
- function help_text() {
- # Output the script help text
- clear
- cat "$0" | head -n 63 | tail -n +2 | tr -d '#'
- echo ""
- exit 0
- }
- # VALIDATION AND OPERATIONS EXECUTION
- if [[ "$@" =~ "-h" || "$@" =~ "-help" || "$@" =~ "--help" ]];
- then
- # Help text filter
- help_text
- fi
- # Initial dependancy check
- depends_check
- # Operational filters
- if [[ "$@" =~ "-s" || "$@" =~ "--silent" || "$@" =~ "-silent" ]];
- then
- OBX="/dev/null"
- fi
- if [[ "x$1" == "x" ]];
- then
- main_process
- final_output
- else
- if [[ "$@" =~ "--test" || "$@" =~ "-test" || "$@" =~ "-t" ]];
- then
- # Allow for a test-run to see what the variables are being set to
- test_function
- fi
- if [[ -d "$1" ]];
- then
- # If the user wants to use a different directory (other than the current one)
- # the core variables must be re-declared to reflect accordingly:
- # Unset arrays first
- unset FILES
- unset VALID_FILES
- # Reset arrays with altered values
- TARGET_DIR="$1"
- FILES=( $(ls ${TARGET_DIR}) )
- VALID_FILES=( $( for i in ${FILES[@]}; do if [[ $i =~ .json$ ]]; then echo $i; fi; done ) )
- VALID_FILES_NUM=$(echo "${#VALID_FILES[@]}")
- # Execute operations
- main_process
- final_output
- fi
- fi
- ################################################################################
- # #
- # "think outside the box" #
- # #
- # ($) ¯\_(ツ)_/¯ (฿) #
- # #
- #############################
RAW Paste Data