aboutsummaryrefslogtreecommitdiffstats
path: root/tools/filter
diff options
context:
space:
mode:
authorJan T <jan@jantuomi.fi>2025-11-13 11:31:30 +0000
committerJan Tuomi <jan@jantuomi.fi>2025-11-13 13:34:38 +0200
commit3df80b5afbebbf3af63b49fe98a3faecd2b68439 (patch)
tree3db37a25de23b7a6b21b3df261b84dc1359c1555 /tools/filter
Initial commit
Diffstat (limited to 'tools/filter')
-rwxr-xr-xtools/filter25
1 files changed, 25 insertions, 0 deletions
diff --git a/tools/filter b/tools/filter
new file mode 100755
index 0000000..94258d0
--- /dev/null
+++ b/tools/filter
@@ -0,0 +1,25 @@
+#!/bin/sh
+
+if [ -z "$1" ]; then
+ echo "Usage: $0 <command>"
+ echo ""
+ echo "Run the given test expression for each newline-separated line in stdin, and echo only the lines that return zero."
+ echo "(Think filter in many programming languages)."
+ echo 'The iterator variable is called "$ELEM", and the index variable is called "$IDX".'
+ exit 1
+fi
+
+i=0
+while IFS=$'\n' read -r line; do
+ j=0
+ for item in $line; do
+ eval "X${j}='$item'"
+ j=$((j+1))
+ done
+
+ IDX=$i ELEM="$line" eval "test $1"
+ if [ $? -eq 0 ]; then
+ echo "$line"
+ fi
+ i=$((i+1))
+done