Small shell tools for text editing

Slashdot it! Delicious Share on Facebook Tweet! Digg!
Bodyaev Dmitry, 123RF.com

Bodyaev Dmitry, 123RF.com

Text Tools

With the jack-of-all-trades sed, you can certainly master any kind of text manipulation, but the result is often complex and hard-to-remember syntax. The helpers presented in this article replace or enhance sed for many smaller text editing tasks.

Many small programs can help you with your "word tasks" in the shell. Table 1 shows an overview of which ones to use. Unless indicated otherwise, they work with single or multiple files as well as pipe operations. Some applications overlap in their functionality.

Table 1

To follow along with the examples shown in this article, you can grab the files a.txt , b.txt , c.txt , d.txt , and e.txt from our FTP server [1].

Showing, Piping, and

Merging

The cat and tac tools differ mainly by the output order of the content: cat puts the file in its original order, and tac reverses the order. The scope of their options is also different (see Table 2). You can also add line numbers using cat (Figure 1), including blank lines.

Figure 1: Using cat and tac, output of a text file as standard out.
Table 2

Instead of sending output to the screen, you can send the content of a text file to another application for further processing. You can do this with both cat and tac .

In the following example, the wc command (described later) counts the lines that cat passes on to it:

cat a.txt | wc -l

For vertical or serial merging of text files (i.e., appending one file after the other), you can pass on the filenames as parameters and, optionally, a target file to copy the result into the following format:

cat [FILE1] [FILE2] ... > [TARGETFILE]

The target file isn't necessary if you want to pass the data through a pipe or to the screen.

Buy this article as PDF

Express-Checkout as PDF

Pages: 1

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content