awk

My Note

  • Print column
bash
awk '{ print $2 }' input.csv
  • Change record separator
bash
awk -F "," '{ print $2 }' input.csv
awk 'BEGIN { FS = "," } { print $2 }' input.csv
  • Print all lines longer than specific length
bash
awk '{ length($0) > 5; }' file.txt
  • Remove duplicates from CSV
bash
awk '{if (!($0 in x)) {print $0; x[$0]=1} }' input.csv > output.csv
  • Mac OS X value comparison
bash
# Wrong
# awk -F"," '$1 == "hello_world" { print $1 } some_file.csv'

# Correct
awk -F"," '$1 ~ "hello_world" { print $1 }' some_file.csv
  • Remove commas in CSV of quotes (“hello, wow” -> “hello wow”)
bash
awk -F'"' -v OFS='' '{ for (i=2; i<=NF; i+=2) gsub(",", "", $i) } 1' some_file.csv
  • Column unique value counts
bash
awk -F, '{ print $1 }' * | sort | uniq -c | sort -nr