Linux is a great system for modifying text on the command line because there are so many tools that can change text. Many have been around since the days of Unix. Here are some tools every Linux user should know about.

cut: Select fields

cut is a rudimentary utility that can take columns of tabular data and display one or a few of them. The -f options selects one field, multiple fields, or ranges. The default delimiter is a tab, but you can change that with the -d option. For example, to select fields 1 and 3 using a space as a delimiter

While cut is serviceable, there are better tools available mentioned below. awk is a powerful tool for pulling apart tabular plain text data. It’s also a lot smarter about white space, being able to pick out and manipulate fields more easily.

Using the cat command on a list of words in a text file, followed by the same filed sorted using the sort command in the Linux terminal.

sort: Sort Text in the Terminal

The sort command sorts any text it receives. The usual method is that lines with numbers come first and are sorted in order, followed by lines starting with letters, which are sorted in alphabetical order. You can change this with options. You can use -d for “dictionary order.”

sort is used most effectively inpipelinessuch as this:

Another useful option is -i to ignore case and -n to force a numerical sort. There are lots more options in the manual page, such as for theGNU version.

uniq: Remove Duplicates

uniq is useful for removing duplicates from text. In other words, it sifts out the “unique” items, hence its name. When you run text through uniq, either a text file as an argument like “uniq file” or from standard input, it will print the text stream to the screen unless redirected as a file with any duplicate entries omitted.

You’ll get back:

The word “apple” now appears only once.

As with the other utilities, it shines when you use it in pipelines working onstandard I/O:

you’re able to see how effective this technique of stringing commands together in a pipeline can be from Brian Kernighan, one of the original Unix developers at Bell Labs and one of the co-authors of the famous book, “The C Programming Language.”

Outputting a text file with duplicate words in the Linux terminal using cat, then removing them with the uniq command.

You can see him MacGyver a spell checker using these utilities right in the terminal in this video from 1982 starting at 5:15:

tr: Replace characters

tr is a utility that lets you replace individual characters in text. Again, this is most useful in a text stream. You can replace a specific character, such as a with c, or a range of characters.

tr’s default arguments are the characters you want to replace and what you want to replace them with.

Using the tr command to change a text file to uppercase in the Linux terminal.

For example, if you wanted to capitalize every lowercase letter in a text stream, you’d use this command: This tells tr to take any lowercase character in the range “a” through “z” and change it into its uppercase counterpart. Enclose the patterns you want in single quotes so the shell knows that you want tr to handle them. Otherwise, the shell will give a syntax error.

If you wanted to convert to lowercase, you could reverse the order:

Replacing the word “apple” with “orange” in a text file with sed in the Linux terminal.

sed: Find and Replace Text

If you want more powerful text replacement, sed is one utility you should look at. It’s too complicated to go into all its features, but I’ll show one of its most popular uses, searching and replacing text with regular expressions.

Regular expressionsare a powerful way of searching for text that lets you specify searches down to the character. In this example, we’ll replace Windows with Linux:

Using the awk command with input from the ps command to pick out username and command fields in the Linux terminal.

This command tells sed to look for the pattern of characters “Linux” and replace them with the expression “Windows.” Regular expressions are widely used in Linux, including in thegrep commandas well as many editors, so they’re essential if you’re serious about using Linux utilities.

Again, put the commands in single quotes so they go to sed instead of the shell.

Editing a list of words in Vim in the Linux terminal.

awk: Powerful Pattern Scanning

awk is a powerful command that’s really a programming language in itself. It was originally named after its creators, Alfred Aho, Peter Weinberger, and our friend Brian Kernighan.

It’s well suited to picking out patterns in text streams. For example, if we wanted to print the users and the commands they’re running, we’d use this pipeline:

This tells awk to take the output of the ps command and print the first and 11th fields. There’s a lot more of awk than can be covered in this section, but you can see how useful it is.

Your Favorite Text Editor: Pick Your Weapon

Perhaps the most important text editing tool isyour text editor. It seems everyone is passionate about their choice of editor. The classic “editor wars” are between Emacs and Vi, or rather Vim. If you’re serious about Linux, this is where you’ll spend a big chunk of your time. If you haven’t decided on one, try several and see how you like them.

Many editors have their own feature set. A lot of the decision comes down to taste. I just find the vi command style of Vim most comfortable for my fingers, but your experience might be different.