Work the Shell - Looking More Closely at Letter and Word Usage

 in
More examples of using the shell to find the frequency of letters used in the English language.
______________________

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.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

missing the n in the newline substitution

Anonymous's picture

$ 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 ...

Mitch Frazier's picture

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:

newline='
'

...

cat dracula.txt | tr ' ' "$newline" | grep ...

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:

$ cat dracula.txt | tr ' ' '
' | grep -v '[^[:alpha:]]' | grep -v "^$"

Mitch Frazier is an Associate Editor for Linux Journal.

Webcast
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers

Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.

Learn More

Sponsored by AMD

White Paper
Red Hat White Paper: Using an Open Source Framework to Catch the Bad Guy

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.

Learn More

Sponsored by DLT Solutions