~/snippets/bash-traverse-files-and-dedup-field
Published on

Traverse Files and Dedup Field

452 words3 min read
#!/bin/bash

cd $(dirname "$0")

PLAYGROUND=()
STAGING=()
PRODUCTION=()

for file in ./k8s/playground/*; do
  for host in $(cat "$file" | dasel -r yaml -w json | jq -r 'select(.spec.hosts != null) | .spec.hosts | .[]'); do
    PLAYGROUND+=("$host")
  done
done

for file in ./k8s/staging/*; do
  for host in $(cat "$file" | dasel -r yaml -w json | jq -r 'select(.spec.hosts != null) | .spec.hosts | .[]'); do
    STAGING+=("$host")
  done
done

for file in ./k8s/production/*; do
  for host in $(cat "$file" | dasel -r yaml -w json | jq -r 'select(.spec.hosts != null) | .spec.hosts | .[]'); do
    PRODUCTION+=("$host")
  done
done

# Remove Duplicates
PLAYGROUND=($(echo "${PLAYGROUND[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
STAGING=($(echo "${STAGING[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
PRODUCTION=($(echo "${PRODUCTION[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))

echo "Playground"
echo "============"
echo "${PLAYGROUND[@]}"

echo "Staging"
echo "============"
echo "${STAGING[@]}"

echo "Production"
echo "============"
echo "${PRODUCTION[@]}"