More on grep

boolean grepping

wildcards and the like

enumerated repetitions and remembered strings

other? send suggestions to david.dailey@sru.edu


Note: instead of using w=/homes/share/words-english for the following, consider using w=/usr/share/dict/linux.words instead, since the former file does not exist on obsidian.

Boolean combinations of sets

disjunction:

command/output explanation
$ w=/homes/share/words-english
w is defined to point to the dictionary we're  using
$ grep by $w |wc
81 81 605
we find and count the lines within $w containing 'by'
$ grep By $w|wc
7 7 51
we do the same for 'By'
$ grep [bB]y $w|wc
88 88 656
looking for either 'b' or 'B' followed by 'y'

negation:

command/output explanation
$ wc -l $w
29141 /homes/share/words-english
How many words (lines) are in the whole dictionary? (29141)
$ grep -c e $w
17002
How many contain 'e'? (17002)
$ grep -c ^[^e]*$ $w
12139
How many contain only non-e's from start (^) to end ($)? (12139).
Note: 29141=17002+12139, as well it should!
$ grep -cv e $w
12139
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
/homes/share/words-english
w is defined to point to the dictionary we're  using
$ grep -c d $w
5938
$ grep -ic d $w
6190
$ grep -c t $w
11766
$ grep -ic t $w
11967
how many words have d? t? ignoring case?
$ grep -i d $w|grep -ic t
1913
how many have both?
$ grep -ic [dt] $w
16244
$ grep -icv [dt] $w
12897
$ grep -ic ^[^dt]*$ $w
12897
how many have either? neither?

grepvenn.gif (9412 bytes)
Logical connectives with grep


diff and using the -i flag on grep:

command/output explanation
$ echo $w
/homes/share/words-english
w has been defined to point to the dictionary we've been using
$ grep by $w |wc
81 81 605
we find and count the lines within $w containing 'by'
$ grep -i by $w |wc
88 88 656
we do the same, ignoring case
$ grep [bB][yY] $w|wc
88 88 656
another way to ignore case (in this instance)
$ grep by $w > bys
$ grep -i by $w >Bys
$ diff bys Bys
5a6,12
> Byers
> Byrd
> Byrne
> Byron
> Byronic
> Byzantine
> Byzantium
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
15 15 138
$ grep ^d.*ty$ $w|wc
49 49 492
$grep ^[bd].*ty$ $w|wc
64 64 630
$grep ^[bd].*ty$ $w|shuf -n5
dexterity
brevity
bestiality
desirability
dishonesty
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
606 606 3582
608 608 3479
we find and count words composed entirely of the letters in the first half of the alphabet..
$grep ^[n-z]*$ $w|wc
237 237 1276
we do the same, for the second half of the alphabet
$paste <(grep ^[a-m]*$ $w|head -5) <(grep ^[a-m]*$ $w|tail -5)
a milk
aback milkmaid
abba mill
abide mime
abigail mimic
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)
no you
nonporous your
nonstop yours
noon zoo
nor zoot
...and in the second set
$ echo text with  varying  spaces|sed 's/\ \ */\ /g'
text with varying spaces
dealing with space characters
$ echo -e "a\tb"
a b
inserting a tab character -- the -e enables interpretation of special chars

complex regular expressions -- counting and remembering

command/output explanation
$echo $u
/usr/share/dict/linux.words
~$grep "[aeiou]\{5\}" $u
cadiueio
Chaouia
cooeeing
euouae
Guauaenok
miaoued
miaouing
Pauiie
queueing
Find words having five vowels in a row (in a big 'dictionary').
$grep "a[bcdfghjklmnpqrstvwxz]\{5\}" $w
angstrom
archtraitor
backscratcher
backstroke
breaststroke
latchstring
packthread
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
atherosclerosis
bandstands beriberi hodgepodge Kinnickinnic
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
atherosclerosis
bandstands
beriberi
hodgepodge
Kinnickinnic
A shorter way to do the same thing, using only one remembered string.
$ echo $w
/homes/ddailey/public_html/words
$ grep "\(...\)\(.*\1\)\{2\}" $w
hemidemisemiquaver
successlessness
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&eacute;bouch&eacute;
higgledy-piggledy
r&eacute;sum&eacute;
twenty-twenty
~$grep "\(..\).*\(..\).*\(..\).*\1.*\2.*\3" $w
d&eacute;bouch&eacute;
higgledy-piggledy
prittle-prattle
r&eacute;sum&eacute;
twenty-twenty
Why is prittle-prattle in one set and not in the other?
What is r&eacute;sum&eacute; ?logh