فهرست منبع

addd no,all,any grep aliases

George Jones 6 ماه پیش
والد
کامیت
86c6fd0c57
3فایلهای تغییر یافته به همراه51 افزوده شده و 4 حذف شده
  1. 2 2
      rc.local/all.sh
  2. 47 0
      rc.local/any.sh
  3. 2 2
      rc.local/no.sh

+ 2 - 2
rc.local/all.sh

@@ -1,6 +1,6 @@
-# read stdin, grep, include lines containing all words on the command line
-
 all() {
+    # read stdin, grep, include lines containing all words on the command line
+    #
     # Usage: all [grep_flags] "word1" "word2" ...
     #
     # Example usage:

+ 47 - 0
rc.local/any.sh

@@ -0,0 +1,47 @@
+any() {
+    # read stdin, grep for any of the words on the command line
+    #
+    # Usage: any [grep_flags] "word1" "word2" ...
+    #
+    # Example usage:
+    # cat some_file.txt | any -i "word1" "word2"
+    #
+    #
+    # Insipred by Marcus Ranum's "Artificial Ignornace":
+    # https://www.ranum.com/security/computer_security/papers/ai/
+    #
+    # source this file to define the alias.
+
+    local words=()
+    local grep_flags=()
+
+    # Separate grep flags from words
+    while [ $# -gt 0 ]; do
+        case "$1" in
+            -*)
+                # Argument is a flag (starts with -)
+                grep_flags+=("$1")
+                shift
+                ;;
+            *)
+                # Argument is a word
+                words+=("$1")
+                shift
+                ;;
+        esac
+    done
+
+    # Construct a grep pattern that matches any of the given words
+    local grep_pattern=""
+    for word in "${words[@]}"; do
+        if [ -n "$grep_pattern" ]; then
+            grep_pattern="${grep_pattern}|${word}"
+        else
+            grep_pattern="${word}"
+        fi
+    done
+
+    # Use grep with specified flags to filter include lines that contain
+    # any of the specified words
+    grep "${grep_flags[@]}" -E "($grep_pattern)"
+}

+ 2 - 2
rc.local/no.sh

@@ -1,6 +1,6 @@
-# read stdin, grep out all words on the command line
-
 no() {
+    # read stdin, grep out all words on the command line
+    #
     # Usage: no [grep_flags] "word1" "word2" ...
     #
     # Example usage: