forked from mikelward/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter
More file actions
executable file
·29 lines (27 loc) · 833 Bytes
/
filter
File metadata and controls
executable file
·29 lines (27 loc) · 833 Bytes
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
#!/bin/bash
# Send matching lines to be processed, pass non-matching lines thru unprocessed.
# Example: </etc/fstab filter "^[^#]" column -t
if test $# -lt 2; then
printf "Usage: filter <pattern> <command> [<command args>]\n" >&2
exit 2
fi
# Decorate, sort, then undecorate lines to ensure line order is preserved, even
# if the command uses different buffering (e.g. only produces output after
# reading all input).
# Undecorate using whitespace rather than just tabs so we can use "column" as
# the command (column converts the tab separator into spaces).
pattern=$1
shift
i=0
{
while read -r line; do
i=$((i+1))
if [[ "$line" =~ $pattern ]]; then
printf "%d\t%s\n" "$i" "$line" >&3
else
printf "%d\t%s\n" "$i" "$line"
fi
done 3> >("$@")
} |
sort -k1n |
sed 's/^[0-9][0-9]*[[:space:]]*//'