0PricingLogin
Linux Command Line & Bash Scripting Mastery · Lesson

Records, Fields, and Custom Separators in awk

Control input and output field separators to slice CSV, TSV, and log lines into clean columns.

What Are Records and Fields in awk?

awk reads input line by line. Each line is called a record, and each whitespace-separated chunk within that line is called a field.

  • $0 — the entire current record (the full line)
  • $1 — the first field
  • $2 — the second field
  • $NF — the last field (NF = number of fields)

By default, awk splits fields on any run of whitespace (spaces or tabs). This makes it immediately useful for log parsing, command output, and space-delimited data.

#!/usr/bin/env bash
# Show how awk sees records and fields
echo 'alice 30 engineer' | awk '{ print "Name:", $1, "Age:", $2, "Role:", $3 }'

# $NF is always the last field — regardless of how many there are
echo 'one two three four five' | awk '{ print "Last field:", $NF }'

The Input Field Separator: FS

The built-in variable FS (Field Separator) tells awk how to split each record into fields. Its default value is a single space, which triggers whitespace-splitting mode.

You can set FS in two ways:

  • With the -F flag on the command line: awk -F':'
  • Inside a BEGIN block: BEGIN { FS = ":" }

Both are equivalent. The BEGIN block approach is preferred in longer scripts because it keeps configuration inside the script itself, making it more readable and portable.

#!/usr/bin/env bash
# Parse /etc/passwd using ':' as the field separator
# Field 1 = username, field 3 = UID, field 7 = shell
echo 'root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin' \
  | awk -F':' '{ print $1, "UID="$3, "shell="$7 }'

All lessons in this course

  1. Records, Fields, and Custom Separators in awk
  2. Patterns, Ranges, and BEGIN/END Blocks
  3. Aggregation with awk Arrays and Grouping
  4. awk Functions, printf Formatting, and Report Generation
← Back to Linux Command Line & Bash Scripting Mastery