Small shell tools for text editing

Slashdot it! Delicious Share on Facebook Tweet! Digg!

Heads and Tails

Using head , you output the beginning of a text file, and with tail , you output the end. These programs provide slightly different options, as described in Table 3. You can use both commands for text files or pipes.

Table 3

In practice, you'll mainly want to read a specific number of lines at the beginning or end of the file. Use the following to output the first five lines:

head -n5 [TEXTFILE]

Or, in the shortened form, use:

head -5 [TEXTFILE]

You could also possibly use:

...| head -n5 .....
...| head -5 ......

You can get the last five lines with:

tail -n5 [TEXTFILE]
tail -5 [TEXTFILE]
...| tail -5 .......

For the tail command, there is yet another interesting option. You can show the ongoing changes to a file using -f (follow ), which is especially useful for log files. You can also set the sleep interval between readings (Figure 2). Pressing Ctrl+C ends the output.

Figure 2: You can keep tabs on changes in a log file using tail -f.

Both programs also let you output specific lines that aren't tagged with a line number. For example, you can do:

tail -3 [TEXTFILE] | head -1

to output just the third line from the end.

Substring Extraction

In most cases, you would use the cut program in structured text files by extracting individual data fields. The output is in the order given in the line. Alternatively, you can output by byte or character order. With data fields you need to indicate the field separator. If it's a special character (e.g., tab) you need to escape it with \ .

Table 4 describes the options. You can follow a field, byte, or character number with a minus sign (-) to indicate extracting everything from it to the end of the file. A minus sign between two values indicates a range.

Table 4

Figure 3 shows how cut works. In the first example, you can see how the second field is extracted from the structured line (the field delimiter is the tab character). The second example extracts characters 5 through 8 from each line. Although awk is covered elsewhere in this issue, it's worth mentioning here if only to point out how awk and cut differ. Unlike cut , you can use awk to extract columns of a structured text file in any order.

Figure 3: Extracting data fields and characters using cut.

This functionality could be interesting if you need to merge CSV files. You can set variables inside loops and extract the data (e.g., letter greetings based on recipients). The calls take the following form:

awk '{OFS="[FIELD_DELIMITER"; print $[FIELD], $[FIELD] ...}' [TEXTFILE]

or, you can do:

... | awk '{OFS="[FIELD_DELIMITER"; print $[FIELD], $[FIELD] ...}' ...

Figure 4 shows an example of how an awk call would work within a pipe. The field delimiter I have chosen is a tabulator character (which is the default delimiter awk uses anyway). The two columns of the file will be reversed.

Figure 4: Extracting data fields using awk.

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