2021-07-29 05:44:12 -07:00
|
|
|
#!/usr/bin/env sh
|
2021-07-29 00:37:35 -07:00
|
|
|
# YES_ZSH
|
2021-07-29 05:44:12 -07:00
|
|
|
# YES_BASH
|
|
|
|
# YES_DASH
|
2021-07-29 00:37:35 -07:00
|
|
|
|
2021-07-30 17:57:08 -07:00
|
|
|
sv() { ### @-
|
|
|
|
### collect the lastmost value of every key.
|
2021-08-01 07:10:53 -07:00
|
|
|
### the field separator can be given as its sole argument,
|
|
|
|
### it defaults to a single space otherwise.
|
2021-07-30 17:57:08 -07:00
|
|
|
###
|
|
|
|
### ```
|
2021-08-01 07:10:53 -07:00
|
|
|
### $ echo "alpha=first\nbeta=second\nalpha=third" | sv =
|
|
|
|
### alpha=third
|
|
|
|
### beta=second
|
2021-07-30 17:57:08 -07:00
|
|
|
### ```
|
|
|
|
###
|
2021-08-01 07:10:53 -07:00
|
|
|
### this next example uses `sv` to only print the lastmost line
|
|
|
|
### matching a pattern in each file. in other words, it uses
|
|
|
|
### the filename printed by grep as the key in its key-value pairs.
|
|
|
|
###
|
|
|
|
### ```
|
|
|
|
### $ cd ~/play/hash && grep -r 'ing$' . | sv :
|
|
|
|
### ./dic_win32.txt:WriteProfileString
|
|
|
|
### ./cracklib-small.txt:zoning
|
|
|
|
### ./english-words.txt:zooming
|
|
|
|
### ./usernames-125k.txt:flats_gaming
|
|
|
|
### ./cain.txt:zoografting
|
|
|
|
### ./pokemon.txt:Fletchling
|
|
|
|
### ./pokemon8.txt:Fletchling
|
|
|
|
### ```
|
|
|
|
###
|
|
|
|
### **TODO:** rename because busybox(1) sv already exists.
|
2021-07-29 05:44:12 -07:00
|
|
|
[ $# -le 1 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; }
|
2021-07-29 00:37:35 -07:00
|
|
|
awk "-F${1:- }" '
|
|
|
|
NF>1{f[$1]=substr($0,length($1)+1+length(FS))}END{for(k in f)print k FS f[k]}
|
|
|
|
'
|
|
|
|
}
|
|
|
|
|
2021-08-02 13:48:46 -07:00
|
|
|
[ -n "${preload+-}" ] || sv "$@"
|