How to filter rows by a column with awk

Filtered command output can look correct while still matching text from the wrong field, especially when a status, owner, or region value appears in more than one column. Use awk when the condition belongs to a numbered column instead of anywhere on the line.

awk splits each input record into fields according to -F. In a simple comma-delimited file, $1 is the first column, $2 is the second, and a rule such as $2 == “failed” prints only rows whose second field is exactly failed.

The sample steps use a small CSV-style fixture without quoted commas. For CSV exports that contain embedded commas, escaped quotes, or multiline fields, use a CSV-aware parser before passing rows to awk or choose a delimiter that cannot appear inside the data.

Steps to filter rows by a column with awk:

  1. Create a small sample file with a header and mixed status values.
    $ cat > orders.csv <<'EOF'
    order_id,status,region,total
    1001,open,west,42
    1002,failed,east,125
    1003,closed,west,78
    1004,failed,west,64
    EOF
  2. Print rows whose second column equals failed while keeping the header.
    $ awk -F ',' 'NR == 1 || $2 == "failed" { print }' orders.csv
    order_id,status,region,total
    1002,failed,east,125
    1004,failed,west,64

    NR == 1 keeps the header row. $2 == “failed” is a case-sensitive exact string comparison against the second field.

  3. Print only matching data rows when another command or script should not receive the header.
    $ awk -F ',' '$2 == "failed" { print }' orders.csv
    1002,failed,east,125
    1004,failed,west,64
  4. Adjust the field number and match value for the column that matters in the real input.

    Use $3 == “west” to match the region column in the sample file, or change -F ',' when the data uses a different delimiter.

  5. Remove the sample file after testing.
    $ rm orders.csv