Technical skill

Linux command: awk

Pick useful lines and columns from text

Suppose a text file has a date and a number on each line, and you want only the rows whose number is at least 100. awk is a small text-processing language and command-line tool for this kind of job. It reads input as records, usually one line at a time, and splits each record into fields, or pieces of data. By default, whitespace separates the fields.

This example prints the date and number from matching rows:

printf '2026-07-01 120\n2026-07-02 85\n2026-07-03 170\n' |
  awk '$2 >= 100 { print $1, $2 }'

$2 means the second field; $1 means the first. Awk checks the condition for each line and prints those two fields only when the number is at least 100. In an awk program, $0 means the entire current record. A rule has a pattern and an action, written pattern { action }: when the pattern matches, awk performs the action. If a rule has no pattern, it applies to every record; if it has no action, awk prints the record by default.

Count records and choose separators

NF stores the number of fields in the current record, and NR counts the records read so far. BEGIN rules run before awk reads input, and END rules run after it finishes; they are useful for setup and summary output. The rule { print $1 } prints the first field from every record.

FS is the input field separator, the rule awk uses to split a record. OFS is the output field separator used when awk rebuilds a record from fields. For colon-separated text, awk -F: sets the input separator. With the default whitespace splitting, repeated spaces and tabs separate fields rather than preserving fixed-width columns.

Assigning a new value to a field changes the record awk prints:

printf 'Hello Tom!\n' | awk '{$2 = "Kitty"; print}'

The output is Hello Kitty!. Awk rebuilds the record from its fields, so the original spacing may be normalized using OFS. If exact whitespace matters, work on $0 with string functions or regular expressions instead of assigning to a field.

Check the input format and implementation

POSIX awk defines a portable core language. GNU awk, usually called gawk, and other implementations add extensions, so code that uses an extension may not run on every Unix-like system. Use the POSIX subset when portability matters and check the manual for the installed version.

Setting FS="," does not make awk a complete CSV parser. CSV (comma-separated values) can contain commas inside quoted fields, escaped quotes, or line breaks inside a field; use a CSV-aware parser for those files. sed is a stream editor for substitutions and line edits. Awk focuses on matching records, fields, variables, and actions; the tools overlap on some tasks but do not behave identically. For a report, state the expected input format and decide how blank or malformed lines should be handled.

Interactive exercises

Skillsets
Skillset PostgreSQL DBA preview
image descriptionPostgreSQL DBA
by Artem Naumenko (fb)
1630 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful