AWK Medium · Lesson 1 · On the house

Records: RS, RT, ORS and paragraph mode

Split input on something other than newlines: single-character and regex RS, RT, blank-line paragraph mode with FS = "\n", and ORS on output. Lesson 1 is free — on the house.

Lessons · AWK Medium · Lesson 1 of 10 · On the house

Records: RS, RT, ORS and paragraph mode

Split input on something other than newlines: single-character and regex RS, RT, blank-line paragraph mode with FS = "\n", and ORS on output.

Illustration for AWK
Lesson 1 is on the house.

RS is the record separator, a newline by default. AWK reads up to the next RS, strips it, and the rest becomes $0. A single-character RS such as RS = ";" splits the input on that character instead, newlines included in the record.

In gawk, an RS longer than one character is a regular expression: RS = "[;|]+" ends a record at any run of semicolons or bars. gawk then sets RT to the exact text that ended the current record, so ; and || can be told apart. POSIX leaves multi-character RS unspecified, so mark such scripts as gawk-only.

RS = "" is paragraph mode: records are separated by one or more blank lines, and blank lines at the start and end of the input are ignored. In this mode, when FS is a single character or the default space, a newline also separates fields.

Pair paragraph mode with FS = "\n" to make each line of a block one field. Stanzas like name: Ada / born: 1815 then arrive as one record with $1 and $2 as whole lines, and NR counts blocks, not lines.

A new RS takes effect from the next record read, so set it in BEGIN for the whole input. getline from a file also uses the current RS, which is how the END block in the still reads toks.txt with a different separator.

ORS is written after every print (default newline) and OFS between its comma-separated arguments. printf adds neither, so it is unaffected by both.

records.awk

# records.awk : gawk -f records.awk
BEGIN {
    printf "name: Ada\nborn: 1815\n\n\nname: Alan\nborn: 1912\n" > "people.txt"
    close("people.txt")
    ARGV[ARGC++] = "people.txt"
    RS = ""                                 # paragraph mode: blank lines end records
    FS = "\n"                               # each line becomes one field
}
{
    split($2, b, ": ")
    print NR ": " $1 " | born " b[2] " | NF=" NF
}
END {
    RS = "[;|]+"                            # longer RS is a regex in gawk
    printf "a;b||c;" > "toks.txt"
    close("toks.txt")
    while ((getline t < "toks.txt") > 0)
        printf "[%s] ended by [%s]\n", t, RT   # RT = text that matched RS
    close("toks.txt")
    ORS = " | "                             # output record separator
    print "x"; print "y"
    ORS = "\n"; print "end"
}
Match this when you type.

Quiz

With RS = "", what separates records?

Quiz

In gawk, what does RT hold?

Quiz

Why set FS = "\n" in paragraph mode?

Quiz

What does ORS = " | " change?

Check

Match the still for: Records: RS, RT, ORS and paragraph mode.

Open-book: the answers are on this page. Pass every quiz and check (5) to mark the lesson done. This visit: 0/5.