Speed up script by using awk(1).
authormglocker <mglocker@openbsd.org>
Fri, 23 Aug 2024 15:22:59 +0000 (15:22 +0000)
committermglocker <mglocker@openbsd.org>
Fri, 23 Aug 2024 15:22:59 +0000 (15:22 +0000)
Discussed with deraadt@

games/quiz/naphone2areas.sh

index febfa3f..aac47d3 100755 (executable)
@@ -10,30 +10,16 @@ if [ X"$1" == X"" ]; then
        exit 1
 fi
 
-AC=0
-grep -v '\#' $1 | grep -v '\$' | \
-while T= read -r line; do
-       AC_LAST=$AC
-       AC=`echo $line | cut -d: -f1`
-
-       # skip line if area code isn't numeric
-       CMD=`echo $AC | grep "^[0-9]*$"`
-       if [ $? -eq 1 ]; then
-               continue
-       fi
-
-       # skip line if area code is a duplicate
-       if [ $AC -eq $AC_LAST ]; then
-               continue
-       fi
-
-       C=`echo $line | cut -d: -f2`
-       SP=`echo $line | cut -d: -f3`
-       SPA=`echo $line | cut -d: -f4`
-
-       if [ X"$SPA" == X"" ]; then
-               echo "$AC:$SP:$C"
-       else
-               echo "$AC:$SP|$SPA:$C"
-       fi
-done
+awk '{
+       split($0, a, ":");
+
+       if (a[1] ~ /^[0-9]+$/) {
+               if (last != a[1]) {
+                       if (a[4] == "")
+                               print a[1] ":" a[3] ":" a[2];
+                       else
+                               print a[1] ":" a[3] "|" a[4] ":" a[2];
+               }
+       }
+       last = a[1];
+}' $1