Use bash arrays, pipe from part 1 to part 2

This commit is contained in:
2024-12-05 22:51:53 +08:00
parent b66b2d098e
commit 9f6182ca9f

View File

@ -1,6 +1,6 @@
#!/usr/bin/env bash
rm key-*
{
while read -r LINE
do
GREP_EXPR=$( printf "%s" "$LINE" | tr ',' '|' )
@ -20,30 +20,39 @@ do
# Don't process for valid lines
[[ $PASS_RULES -eq 0 ]] && continue
printf "%s\n" "$LINE"
# Generate a hashmap and rebuild the entire line from rules
CACHED_LINE=$LINE
while [[ $( printf "%s" "$CACHED_LINE" | tr ',' '\n' | wc -c ) -gt 0 ]]
done < input-updates
} | {
while read -r LINE # Generate a hashmap and rebuild the entire line from rules
do
CACHED_LINE=$( printf "%s" "$LINE" | tr ',' '\n' )
while [[ $( printf "%s" "$CACHED_LINE" | wc -c ) -gt 0 ]]
do
# Loop through rules and build all the KVs
rm key-*
DAG_GREP_EXPR=$( printf "%s" "$CACHED_LINE" | tr ',' '|' )
while read -r RULE
do
KEY=$( printf "%s" "$RULE" | cut -f1 -d'|' )
VALUE=$( printf "%s" "$RULE" | cut -f2 -d'|' )
printf "%s" "$VALUE" > key-"$KEY"
done <<< "$( < input-rules grep -E "($DAG_GREP_EXPR)\\|($DAG_GREP_EXPR)" )"
KEY=$( printf "%s" "$CACHED_LINE" | tr ',' '\n' | head -1 )
DAG_GREP_EXPR=$( printf "%s" "$CACHED_LINE" | paste -s -d '|' )
KV_ARRAY=( $( < input-rules grep -E "($DAG_GREP_EXPR)\\|($DAG_GREP_EXPR)" | tr '|' ' ' | paste -s -d ' ') )
# Going through dependencies to find final key
while [[ -f key-$KEY ]]
KEY=${KV_ARRAY[0]}
while true
do
KEY=$( cat "key-$KEY" )
BREAK=0
for (( i=0; i<${#KV_ARRAY[@]}; i+=2 )) # Check if key exists
do
if [[ ${KV_ARRAY[$i]} -eq $KEY ]]
then
KEY=${KV_ARRAY[$i+1]}
BREAK=1
break
fi
done
if [[ $BREAK -eq 1 ]] ; then continue ; else break ; fi
done
printf "%s\n" "$KEY"
CACHED_LINE=$( printf "%s" "$CACHED_LINE" | tr ',' '\n' | grep -v "$KEY" | paste -s -d ',' )
CACHED_LINE=$( printf "%s" "$CACHED_LINE" | grep -v "$KEY" )
done | tac | paste -s -d' '
done < input-updates |
done
} |
tee final-output |
awk '
BEGIN{ total = 0 }