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
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.csvReformat the output with a custom delimiter using
OFS:awk -F, 'BEGIN {OFS="\t"} {print $1, $3}' data.csvSkip the header row by checking the line number:
awk -F, 'NR > 1 {print $1, $3}' data.csvHandle 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.