about summary refs log tree commit diff stats
path: root/bin/fcard
blob: 23edd30330783323cee7f2195005d3b539f263cc (plain) (blame)
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
#!/bin/sh
# flash card substitute
# usage: fcard dict.tsv section
# the -z code is extremely wip, don't use it

while getopts z: opt
do
	case $opt in
	z)	RAND="|grabbag" ;;
	?)	printf '%s\n' "idiot" 1>&2 && exit 2 ;;
	esac
done
shift $((OPTIND - 1))

rand() {
	tr -cd '[:digit:]'</dev/urandom|fold -w 4|head -1
}
grabbag() {
#	digit urandom % number applicable lines
	size="$(wc -l)"
	oldlist="$(cat)"
	newlist=""
	while [ ${a:="$size"} -gt 0]; do
		z="$(($(rand)%a))"
		newlist="$(cut -d ' ' -f "$z"<<-EOF
		$oldlist
		EOF
	) "
		oldlist="$(cut -d ' ' -f "-$((z-1)),$((z+1))-" <<-EOF
		$oldlist
		EOF
	)"
#	add z to space-delim'd list
#	remove from input list
	a=$((a-1))
	done
	printf '%s\n' "$newlist"
}
for num in $(eval 'grep -n "${2:-.}"\$ "$1"|cut -d : -f 1'"$RAND"); do
	sed -n "$num"P "$1" | cut -f 1 | tr '\n' '\t'
	read -r _
	sed -n "$num"P "$1" | cut -f 2 | sed '/^$/D'
	printf '\n'
done