Q1When does the body of BEGIN { ... } run?
awk — Aggregation and Reports
Practice awk: the order BEGIN { } and END { } run in, s += $1 to accumulate column 1 into a total, and counting with NR or { c++ } to build aggregation reports — illustrated and hands-on in a browser terminal.
Running Before and After — BEGIN and END
awk processes each line in turn, but you can put work you want to run once before and after into BEGIN { ... } and END { ... }. BEGIN runs once before the first line is read, and END runs once after all lines have been read. Use them to print a header up front or to print a summary at the very end.
BEGIN runs once before processing, the body runs per line, and END runs once after.| Form | Meaning |
|---|---|
BEGIN { ... } | Run once before the first line is read |
{ ... } | The body run repeatedly, one line at a time |
END { ... } | Run once after all lines have been read |
s += $1 | Keep adding column 1 into the variable s (a running total) |
NR | Lines read so far (the total line count at the end) |
{ c++ } END { print c } | Increment c per line and print the count at the end |
printf 'start\nmiddle\nend\n' > lines.txt # create 3 lines of material
awk 'BEGIN { print "--- report ---" } { print $0 } END { print "rows:", NR }' lines.txt
# a header up front, rows: 3 at the end
Sum and Count — s += $1 and NR
In awk you can use variables without declaring them, and numbers add up directly. Writing { s += $1 } in the body keeps adding each line's first column into the variable s, and END { print s } prints the total at the end. For a count, either print NR (the number of lines read) directly in END, or increment a counter with { c++ } and print it with END { print c }. With this you can build aggregation reports like a sales total or a record count.
s += $1 adds column 1 line by line, and END prints the total of 60.printf '120 mon\n80 tue\n200 wed\n' > sales.txt # create 3 lines with numbers
awk '{ s += $1 } END { print "total:", s }' sales.txt # total: 400
awk 'END { print "days:", NR }' sales.txt # days: 3
Counting with a Counter — { c++ } END { print c }
NR counts every line, but when you want to count only the lines that match a condition, use a counter in the body. { c++ } increments the variable c by 1 for each line read, and END { print c } prints that count. Adding a pattern like /pat/{ c++ } counts only the lines containing a particular string.
/pass/{ c++ } increments c only on matching lines, and END prints the count of 2.| Form | Meaning |
|---|---|
{ c++ } | Increment the variable c by 1 for each line read |
/pattern/{ c++ } | Increment c only on lines containing pattern |
END { print c } | Print the count c once at the end |
printf 'ok pay\nng pay\nok ship\n' > log.txt # create 3 lines with a status
awk '/ok/{ c++ } END { print "ok count:", c }' log.txt # count lines containing ok -> 2
Knowledge Check
Answer each question one by one.
Q2What does awk '{ s += $1 } END { print s }' f show?
Q3What comes out when you print NR in the END block?