Last active
June 21, 2024 16:50
-
-
Save mike-seger/350a3d6be7cbc7ad2a6f3ea546f4c891 to your computer and use it in GitHub Desktop.
data mapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
input_file="$1" | |
declare -A data | |
declare -A items | |
declare -A dates | |
# Read the data and populate arrays | |
while IFS=$'\t' read -r date item count; do | |
if [ "$date" != "date" ]; then | |
items["$item"]=1 | |
dates["$date"]=1 | |
data["$date,$item"]=$count | |
fi | |
done < "$input_file" | |
# Sort the item keys | |
sorted_items=($(printf "%s\n" "${!items[@]}" | sort)) | |
# Prepare the header | |
{ | |
echo -n "date" | |
for item in "${sorted_items[@]}"; do | |
echo -ne "\t$item" | |
done | |
echo | |
} | |
# Generate the output | |
for date in "${!dates[@]}"; do | |
echo -n "$date" | |
for item in "${sorted_items[@]}"; do | |
echo -ne "\t${data["$date,$item"]:-0}" | |
done | |
echo | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment