Console9

How to extract columns from CSV files with awk

Extract specific columns from comma-separated CSV files using awk field separators.

How to extract columns from CSV files with awk

Extract specific columns from comma-separated CSV files using awk field separators.

Prerequisites

  • awk (gawk or mawk) installed.

Step-by-Step: Extract CSV Columns with awk

  1. Set the field separator to comma with -F, and print specific columns. awk extracts columns 1 and 3 from a CSV file:

    awk -F, '{print $1, $3}' data.csv
  2. Reformat the output with a custom delimiter using OFS:

    awk -F, 'BEGIN {OFS="\t"} {print $1, $3}' data.csv
  3. Skip the header row by checking the line number:

    awk -F, 'NR > 1 {print $1, $3}' data.csv
  4. Handle CSV files with quoted fields containing commas by using FPAT (gawk only):

    gawk 'BEGIN {FPAT="[^,]*|\"[^\"]*\""} {print $2}' data.csv

Common Issues When Extracting CSV with awk

Fields containing commas inside quotes break simple -F, parsing. Use gawk's FPAT or a dedicated CSV tool like csvtool or Python's csv module for complex CSV files.