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,49 +1,58 @@
#!/usr/bin/env bash #!/usr/bin/env bash
rm key-* {
while read -r LINE while read -r LINE
do
GREP_EXPR=$( printf "%s" "$LINE" | tr ',' '|' )
PASS_RULES=0
# Loop through rules and find a breaking rule
while read -r RULE
do do
GREPC=$( printf "%s" "$LINE" | grep -c "$RULE" ) GREP_EXPR=$( printf "%s" "$LINE" | tr ',' '|' )
if [[ $GREPC -ne 1 ]] PASS_RULES=0
then # Loop through rules and find a breaking rule
#printf "%s rule violated for %s \n" "$RULE" "$LINE"
PASS_RULES=1
break
fi
# Filter rules that only has included pages
done <<< "$( < input-rules grep -E "($GREP_EXPR)\\|($GREP_EXPR)" | sed 's/|/,.*/')"
# Don't process for valid lines
[[ $PASS_RULES -eq 0 ]] && continue
# Generate a hashmap and rebuild the entire line from rules
CACHED_LINE=$LINE
while [[ $( printf "%s" "$CACHED_LINE" | tr ',' '\n' | 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 while read -r RULE
do do
KEY=$( printf "%s" "$RULE" | cut -f1 -d'|' ) GREPC=$( printf "%s" "$LINE" | grep -c "$RULE" )
VALUE=$( printf "%s" "$RULE" | cut -f2 -d'|' ) if [[ $GREPC -ne 1 ]]
printf "%s" "$VALUE" > key-"$KEY" then
done <<< "$( < input-rules grep -E "($DAG_GREP_EXPR)\\|($DAG_GREP_EXPR)" )" #printf "%s rule violated for %s \n" "$RULE" "$LINE"
KEY=$( printf "%s" "$CACHED_LINE" | tr ',' '\n' | head -1 ) PASS_RULES=1
# Going through dependencies to find final key break
while [[ -f key-$KEY ]] fi
# Filter rules that only has included pages
done <<< "$( < input-rules grep -E "($GREP_EXPR)\\|($GREP_EXPR)" | sed 's/|/,.*/')"
# Don't process for valid lines
[[ $PASS_RULES -eq 0 ]] && continue
printf "%s\n" "$LINE"
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 do
KEY=$( cat "key-$KEY" ) # Loop through rules and build all the KVs
done DAG_GREP_EXPR=$( printf "%s" "$CACHED_LINE" | paste -s -d '|' )
printf "%s\n" "$KEY" KV_ARRAY=( $( < input-rules grep -E "($DAG_GREP_EXPR)\\|($DAG_GREP_EXPR)" | tr '|' ' ' | paste -s -d ' ') )
CACHED_LINE=$( printf "%s" "$CACHED_LINE" | tr ',' '\n' | grep -v "$KEY" | paste -s -d ',' ) # Going through dependencies to find final key
done | tac | paste -s -d' ' KEY=${KV_ARRAY[0]}
done < input-updates | while true
do
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" | grep -v "$KEY" )
done | tac | paste -s -d' '
done
} |
tee final-output | tee final-output |
awk ' awk '
BEGIN{ total = 0 } BEGIN{ total = 0 }