How to Print Specific Columns and Fields Using awk in Linux

When you are analyzing a massive, delimited dataset (e.g., a 10GB CSV file or a system process log) on a Linux server, and you only require data from the 2nd and 5th columns, using standard text extraction tools like grep is mathematically insufficient, as they operate on entire lines. To force the Linux kernel to execute a column-aware geometric extraction—ignoring all other data structures and printing only your target fields—you must deploy the awk language engine.

Executing the Column Extraction Calculus

The awk engine is a complete, Turing-complete data processing language natively built into Linux. It automatically parses every incoming text line into an array of discrete fields based on a delimiter (defaulting to whitespace).

Imagine you have a massive file named employee_records.txt (where columns are separated by spaces: ID, First Name, Last Name, Department, Salary). You only want to extract the Last Name (column 3) and the Salary (column 5).

To execute the extraction vector, open your terminal and type the precise command:

awk '{print $3, $5}' employee_records.txt

Analyzing the Field Matrix

The exact millisecond you press Enter, the awk engine intercepts the data payload.

  • It reads line 1 and mathematically splinters it into individual variables: $1 (ID), $2 (First Name), $3 (Last Name), etc.
  • The print $3, $5 instruction commands the engine to extract only those two specific variables.
  • It then prints them to standard output, separated by a single space (the comma in the awk syntax dictates the default output separator).
  • It violently dumps the rest of the variables ($1, $2, $4) into the memory void.
  • It repeats this calculus for every single line in the file.
  • (Critical Note: If your file is a CSV and uses commas instead of spaces as delimiters, you must explicitly declare the separator using the -F flag: awk -F',' '{print $3, $5}' file.csv).

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.