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.
Related: How to print selected columns with awk
Related: How to count rows by field with awk
Steps to filter rows by a column with awk:
- 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
- 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,64NR == 1 keeps the header row. $2 == “failed” is a case-sensitive exact string comparison against the second field.
- 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 - 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.
- Remove the sample file after testing.
$ rm orders.csv
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.