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 #!/usr/bin/env bash
rm key-* {
while read -r LINE while read -r LINE
do do
GREP_EXPR=$( printf "%s" "$LINE" | tr ',' '|' ) GREP_EXPR=$( printf "%s" "$LINE" | tr ',' '|' )
@ -20,30 +20,39 @@ do
# Don't process for valid lines # Don't process for valid lines
[[ $PASS_RULES -eq 0 ]] && continue [[ $PASS_RULES -eq 0 ]] && continue
printf "%s\n" "$LINE"
# Generate a hashmap and rebuild the entire line from rules done < input-updates
CACHED_LINE=$LINE } | {
while [[ $( printf "%s" "$CACHED_LINE" | tr ',' '\n' | wc -c ) -gt 0 ]] 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 do
# Loop through rules and build all the KVs # Loop through rules and build all the KVs
rm key-* DAG_GREP_EXPR=$( printf "%s" "$CACHED_LINE" | paste -s -d '|' )
DAG_GREP_EXPR=$( printf "%s" "$CACHED_LINE" | tr ',' '|' ) KV_ARRAY=( $( < input-rules grep -E "($DAG_GREP_EXPR)\\|($DAG_GREP_EXPR)" | tr '|' ' ' | paste -s -d ' ') )
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 )
# Going through dependencies to find final key # Going through dependencies to find final key
while [[ -f key-$KEY ]] KEY=${KV_ARRAY[0]}
while true
do 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 done
printf "%s\n" "$KEY" 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 | tac | paste -s -d' '
done < input-updates | done
} |
tee final-output | tee final-output |
awk ' awk '
BEGIN{ total = 0 } BEGIN{ total = 0 }