summaryrefslogtreecommitdiffstats
path: root/services/lib/filter
blob: f69a5b0d35c511c606942a8ca1118713a71dd7ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#! /bin/sh
#
# usage:
#   export PATH="//services/lib:$PATH"
#   cd services
#   ls | filter owner == $LOGNAME | filter hasnt mail
#
set -euf

main() {
  case $# in
    2) op1 "$@";;
    3) op2 "$@";;
    *) echo 'You are made of stupid!' >&2; exit 23;;
  esac
}

# op1 OP SCHEMA
op1() {
  case "$1" in
    has)
      xargs grep -H "^$2:" \
        | cut -d: -f1 
      ;;
    hasnt)
      a=$(mktemp)
      b=$(mktemp)
      trap "rm $a $b; trap - EXIT INT QUIT" EXIT INT QUIT
      cat > $a
      cat $a | xargs grep -H "^$2:" | cut -d: -f1 > $b
      diff -u $b $a | sed -n '/^++/d;s/^+\(.*\)/\1/p' | grep .
  esac
}

# op2 SCHEMA OP RHS
op2() {
  case "$2" in
    ==|is)
      xargs grep -H "^$1:$3$" \
        | cut -d: -f1
      ;;
    !=|isnt)
      xargs grep -H "^$1:" \
        | grep -v ":$1:$3" \
        | cut -d: -f1
      ;;
    contains)
      xargs grep -H "^$1:.*$3.*$" \
        | cut -d: -f1
      ;;
  esac
}

main "$@"