Given its heritage in Unix, it’s no surprise that GNU has a wealth of text processing tools and techniques to aid coders, writers, and publishers in editing and otherwise manipulating text documents.
After abandoning the Multics project, Bell Labs began seeking to create an operating system that would help them to manage the wealth of electronic documents that they had accumulated since the dawn of the digital age. They commissioned their Multics developer, Ken Thompson, to begin work on such a system. Eventually, that system would become known as Unix. Unix would go on to become very popular, so, when Richard Stallman decided to create the complete, free operating system, GNU, he modeled it after Unix. Hence the abundance of text processing tools, utilities, and techniques supported by the GNU system.
This three-part series will introduce the bulk of those tools and utilities (plus some techniques) and include links to learning more. This part (part 1A) will cover the basic tools.
To learn the basic functions of any of the commands below type the name of the command followed by “–help”. For example, to learn more about the “less” command you would type (at the command prompt):
$ less –help
(Don’t type the dollar sign “$”, that simply represents the user command prompt.)
Also, if you need more detailed information about any of the following commands you can read their “man” or “info” pages by typing the word “man” or “info” respectively, followed by the command name. To see either manual for the “less” command type:
$ man less
$ info less
Less is a text reader, or (as it’s called in the hackerverse) a pager, so-called because it prints text to the screen one “page” at a time, rather than all at once. It’s used to view the contents of text files. To see how it works type the word “less” followed by the name of a text file. Here’s an example (type this at the command prompt):
$ less name-of-some-text-file
Less was created to fix some of the shortcomings of “more,” the original Unix pager. The name “less” is a play on the phrase “less is more.” Because less does the same thing as more, it pages, but it has more functionality. For instance, less allows one to move backward through the file, in addition to forward.
The advantage less has over editors like nano (see below) or vim, is that it does not have to read the entire input file before opening it, so it starts faster than they do.
Hit the “q” key to exit less and return to the command prompt.
To learn more about less:
- less Man Page
- less FAQ
- Why the “less” command is better than “more”
- Less command: 10 tips for effective navigation
Less only allows you to view text files and not to edit them. For that you need a text editor like nano or emacs (see part 3)
To try it out type “nano” followed by the name of a text file. In other words, type this at the command prompt:
$ nano name-of-text-file
Alternately, you can simply type the word nano to open the editor with an empty file which you can type text into and then save as a new file. Once nano opens the file you can edit the file by adding, removing, or otherwise changing the text. Nano also allows you to find text in the file and there’s even a limited ability to format text.
To close nano and return to the command prompt hold down the “Ctrl” key and hit the “X” key.
To learn more about nano:
- The Beginner’s Guide to nano
- nano Man Page
- nano Manual
- nano FAQ
Before there was “less” (or even “more”) there was “cat.” Cat was — and still is (unfortunately) — used to view text files, similar to how pagers, like “less” and “more,” are used. Cat is a “concatenator” utility, that was designed to connect or link files in a series or chain. Traditionally, it has been used by programmers to create one long source file out of multiple smaller files, but it has other uses. To see cat in action type (at the command prompt) the word “cat” followed by the name of a text file:
$ cat name-of-text-file
This will print the file to your screen for viewing. However, it will dump the whole file all at once and all you will see on your screen is the end of the file. If it happens to be a very short file that isn’t a problem, but for longer files use “less.”
As noted above, cat’s real function is to concatenate or link multiple files together into a single file.
To learn more about cat:
- The cat Command
- HowTo: Use cat Command
- 10 Cat Command Examples to Manage Files
- Concatenate and write files
- cat Man Page
“sort” works similarly to “cat,” in that it will “dump” the entirety of the file to the screen, but it will sort it before printing. As its name suggests, sorting is its primary function, however, it can also be used to merge and compare text as well, and has a number of other functions. To use it, type the word “sort” followed by the name of a text file or text files. Example:
$ sort name-of-text-file
If you want to see one “page” at a time, pipe “sort” through “less”, like this:
$ sort name-of-text-file | less
The “|” character sends the standard output (stdout) of a given command to the the standard input (stdin) of the next command in the pipe, to be processed by that command. (“|” can be used with most, if not all, of the commands in this overview.)
To learn more about sort:
- sort Command Examples
- Sort Text Files
- sort Man Page
Copyright © 2013 Russell James
Permission is granted to copy, distribute and/or modify this document under the terms of the Creative Commons Attribution-ShareAlike 3.0 Unported license.