bashjq

While Loop Read and JQ, use array list of column names


I want to pass a "array" to the while loop so I don't have to repeat the column names

COLUMNS=("SOURCE_FILE" "EXIF_CONTAINER" "EXIF_CODEC" "EXIF_ENCODER")

while IFS=',' read -r SOURCE_FILE EXIF_CONTAINER EXIF_CODEC EXIF_ENCODER
do
   #Do Processing here 
done < <(
   echo "$EXIF_DATA"  | jq -cr '.[] | "\(.SourceFile),\(.EXIF_CONTAINER),\(.EXIF_CODEC), \(.EXIF_ENCODER)"'
)

How do I make the "while loop read" and "jq" use the COLUMNS list instead of having to repeat the column names?


Solution

  • For while read, just expand the array instead of listing all variable names.

    Change

    while IFS=',' read -r SOURCE_FILE EXIF_CONTAINER EXIF_CODEC EXIF_ENCODER
    

    into

    while IFS=',' read -r "${COLUMNS[@]}"
    

    As for jq, use the --args flag to supply multiple arguments at once, populating jq's $ARGS.positional array. Then, here as well, just expand your Bash array as additional arguments.

    Change

    jq -r '.[] | "\(.SOURCE_FILE),\(.EXIF_CONTAINER),\(.EXIF_CODEC),\(.EXIF_ENCODER)"'
    

    into

    jq -r '.[] | [.[$ARGS.positional[]]] | join(",")' --args "${COLUMNS[@]}"
    

    Note: Watch out for upper/lower case column/field names (e.g. SourceFile vs. SOURCE_FILE), and better use lowercase variable names in Bash (e.g. columns instead of COLUMNS)