If you don't know text, you don't know Linux. There are a host of methods for reformatting plain text -- including the text used by graphical applications like spreadsheets and email programs.
Plain text is a series of characters delimited into lines by newline (LF, line feed) characters. You can send this text directly to a terminal window with a utility like cat(1). There are no hidden formatting codes; it’s “just the text, ma’am.”
Before the puns get any worse, let’s dig in!
As you saw in last month’s column (if you didn’t see the column, you might want to review it), to start a new line at any point in plain text, simply insert a newline character. To join two lines, remove the newline between them — and maybe add a space or TAB character to separate them.
When a terminal or printer reads a TAB character, it moves the current position to the next tabstop. TAB characters are also used as field separators; you can make a simple database with TABs between the fields and a newline at the end of each record.
Linux utilities can also reformat text that doesn’t contain TABs. We’ll see examples of that, too.
Lots of Possibilities
Many GNU utilities started in the days of Unix — back when a tty really was a teletype. Without a graphical display (or a graphical editor) to rearrange text, programmers came up with many ways to slice, dice, and reassemble data from scripts and the command line.
We’ll see some of those ways: Enough ways, I hope, that people new to this way of handling text will be ready to find other ways — and gurus will still get a few surprises.
Starting with a Spreadsheet
Plain text can come from lots of places, including:
- The output of a utility (grep, for instance),
- Text saved from an application (see Figure One for an example),
- Text pasted into a terminal window from a graphical application, as in Figure Two near the end of this article.
Note that some of this text may not be “plain” characters. For instance, if you’re copying from a web page designed by a Macintosh user, the designer may have unwittingly included the Macintosh encoding of a special character (maybe a “curly quote”) that isn’t recognized on your Linux system.
For the first few examples, let’s use an OpenOffice.org spreadsheet file saved as plain text. (On the File menu, choose Save As, type Text CSV.) Assuming that the data doesn’t contain any TAB characters, you can set the Field Delimiter to TAB and the Text Delimiter to none (delete the default quote mark in that dialog box). Figure One shows this.
Figure One: Saving a spreadsheet as plain text
Below are are two views of the resulting file data.txt (renamed from the default data.csv). First, plain cat outputs the TAB characters between fields, which the terminal displays by moving to the next tabstop position. Next, cat -tve shows what’s actually in the file:
$ cat data.txt
STATE CITY COUNTY POP. GOVT.
AZ Ely Gila 123 Mayor
CA Alma Lolo 345 Sheriff
TX Leroy El Paso 22 Bubba
$ cat -tve data.txt
Checking the data file with cat -tve or od -c is a good idea. They’ll reveal “hidden” or “non-plain” characters buried in the data. Notice the space character in the field El Paso. Because the field separator is a TAB, the space doesn’t cause any problems.
Utilities that Understand TABs
Scripting languages (Perl, awk, …) can parse and write TAB-separated data. Table One lists some other Linux utilities that handle TABs.
Table One: Some utilities that understand TABs
||Remove sections from each line of files
||Write arguments to standard output (\t makes a TAB)
||Convert TABs to spaces, spaces to TABs
||Merge lines of files into TAB-separated output
||Sort data by one or more of its fields
Whether your data comes from a spreadsheet or some other source, if you can massage your data into TAB-separated fields, the examples below can help you slice and dice it. Examples toward the end of the article cover other types of data.