Working with Multi-Line Records
So far, we’ve been using awk
to parse text files in which each line is its own distinct record. Sometimes though, you might have to work with files that have each record spread across several lines. For example, look at this inventory.txt
file:
Kitchen spatula
$4.99
Housewares
Raincoat
$36.99
Clothing
On Sale!
Claw hammer
$7.99
Tools
The first and third records each consist of three lines, and the second record consists of four lines. Each record is separated by a blank space. Now, let’s say that we need to import this information into a spreadsheet. That won’t work well with multi-line records, so we’ll need to find an easy way to convert it into a spreadsheet-friendly format. Once again, awk
to the rescue! Here’s the inventory.awk
script that helps us out:
#!/usr/bin/awk -f
BEGIN {
FS="\n"
RS=""
ORS=""
}
{
count=1
while (count<NF...