More fun with grepping (using sort and paste)

count the words that have 'v' eventually followed by 'b':

$ grep v.*b /homes/share/words-english | wc -l
94


another way to do the same thing: (grep -c)

$ w=/homes/share/words-english
$ grep -c v.*b $w
94


Let's look at the first five of these 94 words:

$ grep v.*b $w | head -5
November
Novosibirsk
Zxcvb
Zxcvbnm
aboveboard

and the last five:

$ grep v.*b $w | tail -5
volleyball
voluble
vulnerable
wavenumber
zxcvbnm


While we're at it let's put the first and last parts in separate files:

$ grep v.*b $w |head -5 > headvb
$ grep v.*b $w | tail -5 > tailvb

$ ls -l *vb
-rw-r--r-- 1 ddailey faculty 46 Feb 4 15:57 headvb
-rw-r--r-- 1 ddailey faculty 49 Feb 4 15:57 tailvb


examine these files ...

headvb tailvb in reverse order side-by-side
$ cat headvb
November
Novosibirsk
Zxcvb
Zxcvbnm
aboveboard
$ cat tailvb
volleyball
voluble
vulnerable
wavenumber
zxcvbnm
$ sort -r tailvb
zxcvbnm
wavenumber
vulnerable
voluble
volleyball
$ paste headvb tailvb
November volleyball
Novosibirsk voluble
Zxcvb vulnerable
Zxcvbnm wavenumber
aboveboard zxcvbnm

Let's do some more

$ echo $w
/homes/share/words-english
$ grep -c b.*b $w
287


$ grep b.*b $w |tail -12
stubborn
stubby
subbing
submersible
suburb
suburbia
swabbing
swabby
tabbing
throbbing
webbing
wobble

Let's make a file called 'twebb' ontaining the above output:

$ grep b.*b $w |tail -12 > twebb

and another called 'twelve' with the names of the first twelve natural numbers:

$ cat twelve
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve

Let's take a peek at what we've got:

lising all filenames beginning with 'tw'

$ ls -l tw*
-rw------- 1 ddailey faculty 101 Feb 22 15:21 twebb
-rw------- 1 ddailey faculty 63 Oct 4 16:11 twelve

a word count on all files beginning with 'tw'

$ wc tw*
12 12 101 twebb
12 12 63 twelve
24 24 164 total

the last three lines of 'twebb'

$ tail -3 twebb
throbbing
webbing
wobble

the last three lines of 'twelve'

$ tail -3 twelve
ten
eleven
twelve

Let's adjoin these files, on screen:

$ paste twelve twebb
one stubborn
two stubby
three subbing
four submersible
five suburb
six suburbia
seven swabbing
eight swabby
nine tabbing
ten throbbing
eleven webbing
twelve wobble

and into a file:

$ paste twelve twebb > twelvbb


Now practice with sorting the lines of the file: Note that the file itself has been left unchanged:
$ sort twelvbb
eight swabby
eleven webbing
five suburb
four submersible
nine tabbing
one stubborn
seven swabbing
six suburbia
ten throbbing
three subbing
twelve wobble
two stubby
$ cat twelvbb
one stubborn
two stubby
three subbing
four submersible
five suburb
six suburbia
seven swabbing
eight swabby
nine tabbing
ten throbbing
eleven webbing
twelve wobble

we can also sort in reverse order:

$ sort -r twelvbb
two stubby
twelve wobble
three subbing
ten throbbing
six suburbia
seven swabbing
one stubborn
nine tabbing
four submersible
five suburb
eleven webbing
eight swabby
 

note that the file is still unchanged:

[ddailey@marble ddailey]$ tail -2 twelvbb
eleven webbing
twelve wobble


Let's put the reverse sorting of the file twelvbb into a new file:

$ sort -r twelvbb > tweverse
[ddailey@marble ddailey]$ cat tweverse
two stubby
twelve wobble
three subbing
ten throbbing
six suburbia
seven swabbing
one stubborn
nine tabbing
four submersible
five suburb
eleven webbing
eight swabby

Note that the above lines are ordered in reverse alphabetically order based on the first word.

If we sort this file, it simply reverses the order of the lines:

$ sort tweverse
eight swabby
eleven webbing
five suburb
four submersible
nine tabbing
one stubborn
seven swabbing
six suburbia
ten throbbing
three subbing
twelve wobble
two stubby


Now let's alphabetize based on the second column: (note: the old command sort +1 twelbbv appears to have been deprecated)

$ sort -k2 twelbbv
one stubborn
two stubby
three subbing
four submersible
five suburb
six suburbia
seven swabbing
eight swabby
nine tabbing
ten throbbing
eleven webbing
twelve wobble

Note that the first column is now ordered by the numerical value of named integer... why?

One more fun one? -- Extra credit -- since sort + seems to be deprecated,  be the first person to email me a solution showing how to do this nowadays.

$ sort +1.2 tweverse
nine tabbing
eleven webbing
ten throbbing
twelve wobble
one stubborn
two stubby
three subbing
four submersible
five suburb
six suburbia
seven swabbing
eight swabby

Any clue what order these are in? (look at the man file for sort)