Process words at the commandline

No, this tip isn't a rehash of Tip 177, on page 216, which describes how to install Microsoft Word for use at the command-line (yes, really).

instead this tip is a rundown of several useful text-processing tools available at the command-line. The fact is that bash (and sh before it) predate the serious introduction of word processors. in fact, it could be argued that many word processing features evolved from text-manipulation tools built into bash.

Start by consulting Tip 196, on page 235, to find out how to create a text file at the prompt with the minimum of fuss. Assuming you've now created a document, let's move on to look at the tools available to process it.

• Spell checking: As its name suggests, aspell is a spell-checker. To use it, provide the filename of the text file to be checked after the -c option: aspell -c file.txt. Once it's running you'll be presented with a list of alternatives for words that are misspelled. To select one, type the number next to its entry in the list. To skip that word, type T. To quit, type [x]. Bear in mind that the corrected file will overwrite the original once you've finished, although a backup of the file will be created with a .bak file extension.

To spellcheck a single word at the command-line, use the look command: look mississippi, for example. If the word is in the list of results then it's spelled correctly. If it's not in the list then it isn't spelled correctly. Note that a long list of results might be returned because every permutation of the word will be returned. Search for cart, for example, and carton, cartographer, and others will be returned.

• Word count: wc stands for "word count" and, sure enough, can be used to count the words of a specified file (ie wc textfile.txt). Three figures will be reported. The first is the number of lines, the second the number of words, and the third is the filesize (in bytes). Use the -w command option if you just want to know the number of words.

• Word wrap: You can make a document "word wrap" using the fold command. This sets carriage returns at the end of lines, so it isn't quite like the dynamic word wrap function you might be used to in word processors. Yet it's sometimes useful nonetheless. The -w command option is used to set the character count (per line) when the line should be broken. It's also advisable to add the -s command option to stop fold breaking words in half. The following will set a word wrap after approximately 40 characters on each line, creating a new file called wrapped.txt with the changes within it:

Was this article helpful?

0 0

Post a comment