Work the Shell - Looking More Closely at Letter and Word Usage
Time and again I have entreated you, dear readers, with my plea for “A letter! My column, nay, my kingdom for a reader letter!” And lo, the miracle occurred, the heavens parted, the angels sang and a letter arrived:
In addition to the letter and word frequency, how about looking at how frequently a letter appears as the first letter of a word? Just to make things more interesting, what is the frequency of two-letter combinations? For instance, if the first letter of a two-letter combination is a t, what is the most frequent second letter? Thanks for the article in Linux Journal. It was a good read and nice scripts.—Mike Short
Quando omni flunkus moritati.
First off, before I even read the letter, I was intrigued by the closing quote. Latin? Isn't that, like, a dead language? Turns out the quote's a good one though, especially for IT admins in big companies. It roughly translates to “when all else fails, play dead”, and it comes from the Red Green Show, a Canadian comedy. (Thanks Google.)
Now, on to the heart of the letter. Mike's referring to an earlier column where we looked at how to use shell scripts to ascertain letter and word usage, using three books as source material: Dracula, A History of the United States and Pride and Prejudice, all downloaded from Project Gutenberg.
In that series of columns, we ascertained that the ten most common letters in the English language are e, t, a, o, n, i, s, r, h and d. Are they the same if we constrain it to just the first letter of words? Let's find out.
Once we have a corpus of writing and the ability to break it down by words, so that the input stream to the counting script:
is like this
it's done like so:
$ cat dracula.txt | tr ' ' '\ ' | grep -v '[^[:alpha:]]' | grep -v "^$"
That'll turn Dracula into the world's narrowest book, with one word per line.
Now we simply can add to it to axe all but the first letter by appending cut -c1. The result looks like one of those streams of letters in The Matrix, but that's another story.
So, all that's left is to translate uppercase into lowercase, sort, and then use our friend uniq -c to tally up the results:
tr '[:upper:]' '[:lower:]' | sort | uniq -c | sort -rn | head
And, the resultant top ten are:
20648 t 15787 a 11110 i 10655 w 9906 h 9030 s 7618 o 5720 m 5411 b 4597 f
Quite different! Now, the question is, does it change based on the type of content? Let's do the same command, but this time, let's feed in all three of our books, not just Dracula (though with the rabid <cough cough> popularity of Twilight, maybe Linux Journal would do well to stick with a vampire theme for a few issues?):
34359 t 27053 a 18212 w 18119 h 17854 i 15746 s 13614 o 10076 b 9792 m 7712 f
It's not exactly the same. Isn't that interesting? I'm not sure what to make of it, but as you can see, a good grasp of shell script commands makes finding out this sort of fairly goofy information interesting.
But, we're not quite done, because Mike also wondered about two-letter combinations. It's this sort of query that really shows just how helpful becoming savvy on the command line can be. To calculate that requires only one character to be changed in the command invoked above. Do you know what it is?
It's the cut command. Above, we're specifying that we want only the very first character of each line of input with cut -c1. If we want the first two, we simply can tweak that command flag as appropriate.
But, -c2 won't work, because that'll give us only the second letter of each word (and the most common second letter in the English language is o, followed by h, e, a and n).
Instead, we need to use a letter range, which looks like this: -c1-2. The result of that invocation is:
22100 th 10168 an 9138 to 7508 he 7100 of 5873 i<space> 5517 in 5332 ha 5157 be 4664 wh
There ya go, Mike. The most common two-letter combination in the English language is th, which actually makes some sense, followed as a distant second by an.
I hope it's trivially obvious how you could use this to calculate the most common three-letter combinations (it should be no surprise at all that the is the most common three letter combo, followed by and).
I'll wrap up here, but again, I invite you to send me your letters and queries so we can explore various ways to use shell scripts.
Dave Taylor has been involved with UNIX since he first logged in to the on-line network in 1980. That means that, yes, he's coming up to the 30-year mark now. You can find him just about everywhere on-line, but start here: www.DaveTaylorOnline.com.
Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- RSS Feeds
- What's the tweeting protocol?
- New Products
- Trying to Tame the Tablet
- Dart: a New Web Programming Experience
- Reply to comment | Linux Journal
15 hours 22 min ago - Reply to comment | Linux Journal
17 hours 55 min ago - Reply to comment | Linux Journal
19 hours 12 min ago - great post
19 hours 47 min ago - Google Docs
20 hours 10 min ago - Reply to comment | Linux Journal
1 day 58 min ago - Reply to comment | Linux Journal
1 day 1 hour ago - Web Hosting IQ
1 day 3 hours ago - Thanks for taking the time to
1 day 4 hours ago - Linux is good
1 day 6 hours ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




Comments
missing the n in the newline substitution
$ cat dracula.txt | tr ' ' '\
' | grep -v '[^[:alpha:]]' | grep -v "^$"
shoule be:
$ cat dracula.txt | tr ' ' '\n
' | grep -v '[^[:alpha:]]' | grep -v "^$"
unless I am missing something ;)
Bash is not C or Python or ...
Ending a line with a backslash is the correct way to escape a newline in bash. In many languages, such as C, the backslash gives the next character special significance (eg \n causes the n to become a newline in the string). In bash, on the other hand, the backslash removes any special significance that the next character has. So a backslash as the last character on the line causes the next character (a real newline) to lose its special significance (which is to end a command line) and become a regular character.
The newline case is a bit strange for sure and causes some funny looking code sometimes, I tend to do this instead:
That makes it a bit more explicit as to what's happening and you only have to see the "weirdness" once at the top of your script.
Also note, that in Dave's example the newline is not actually required since it appears inside a quoted string. Bash won't recognize a newline as the end of a command if there is an open quoted string, so it just puts the newline into the string. So, the following will work also:
Mitch Frazier is an Associate Editor for Linux Journal.