enumerated repetitions and remembered strings
other? send suggestions to david.dailey@sru.edu
disjunction:
| command/output | explanation |
$ w=/homes/share/words-english |
w is defined to point to the dictionary we're using |
$ grep by $w |wc |
we find and count the lines within $w containing 'by' |
$ grep By $w|wc |
we do the same for 'By' |
$ grep [bB]y $w|wc |
looking for either 'b' or 'B' followed by 'y' |
negation:
| command/output | explanation |
$ wc -l $w |
How many words (lines) are in the whole dictionary? (29141) |
$ grep -c e $w |
How many contain 'e'? (17002) |
$ grep -c ^[^e]*$ $w |
How many contain only non-e's from start (^) to end ($)? (12139). Note: 29141=17002+12139, as well it should! |
$ grep -cv e $w |
the same thing using the -v flag (look for non-matches) on grep. |
combining disjunction, conjunction and/or negation (see diagram below):
| command/output | explanation |
$ echo $w |
w is defined to point to the dictionary we're using |
$ grep -c d $w |
how many words have d? t? ignoring case? |
$ grep -i d $w|grep -ic t |
how many have both? |
$ grep -ic [dt] $w |
how many have either? neither? |

Logical connectives with grep
diff and using the -i flag on grep:
| command/output | explanation |
$ echo $w |
w has been defined to point to the dictionary we've been using |
$ grep by $w |wc |
we find and count the lines within $w containing 'by' |
$ grep -i by $w |wc |
we do the same, ignoring case |
$ grep [bB][yY] $w|wc |
another way to ignore case (in this instance) |
$ grep by $w > bys |
we examine differences by putting output in files and then using the 'diff' command |
fancier stuff with special characters: '^', '$', '.' , '*', spaces and tabs
| command/output | explanation |
$ grep ^b.*ty$ $w|wc |
here we consider the things beginning with either b or d and ending in ty. Note that the numbers add up. In the last case, we show a random sample of five lines of output (using the shuf command). |
$ $grep ^[a-m]*$ $w|wc |
we find and count words composed entirely of the letters in the first half of the alphabet.. |
$grep ^[n-z]*$ $w|wc |
we do the same, for the second half of the alphabet |
$paste <(grep ^[a-m]*$ $w|head -5) <(grep ^[a-m]*$ $w|tail -5) |
we examine some of the output in the first set (the first and last five lines, pasted together) |
$paste <(grep ^[n-z]*$ $w|head -5) <(grep ^[n-z]*$ $w|tail -5) |
...and in the second set |
$ echo text with varying spaces|sed 's/\ \ */\ /g' | dealing with space characters |
$ echo -e "a\tb" | inserting a tab character -- the -e enables interpretation of special chars |
complex regular expressions -- counting and remembering
| command/output | explanation |
$echo $u |
Find words having five vowels in a row (in a big 'dictionary'). |
$grep "a[bcdfghjklmnpqrstvwxz]\{5\}" $w
|
Find words having five consonants in a row, directly following an 'a'. |
| $grep "a[bcdfghjklmnpqrstvwxz]\{5\}" $w|wc 7 7 82 $grep "a[^aeiouy]\{5\}" $w|wc 19 19 223 | Why do these commands have different output? |
$grep "\(.\)\(.\)\(.\)\(.\).*\1\2\3\4" $d |
Find words having a sequence of four letters repeated twice. Each parenthesized string is remembered and recalled through \n. (from an old dictionary that I can't seem to find anymore). |
$grep "\(....\).*\1" $u |
A shorter way to do the same thing, using only one remembered string. |
$ echo $w |
Find a sequence of three characters and remember them. Then find any string followed by those same three characters, and look for two such repetitions of the initial pattern. |
| $grep "\(..\).*\(..\).*\(..\).*\1\2\3" $w débouché higgledy-piggledy résumé twenty-twenty ~$grep "\(..\).*\(..\).*\(..\).*\1.*\2.*\3" $w débouché higgledy-piggledy prittle-prattle résumé twenty-twenty | Why is prittle-prattle in one set and not in the other? What is résumé ?logh |