0

Question: Nice "drop-in" replacements to grep some lines of csv files containing some keywords in some column.

Requirement: support more complex csv files - that have double quotes, newlines.

Usecase: "csvkit" is awesome, but it might be too much to just grep few lines.

There is very nice csvkit for working with csv files, that includes csvgrep. But sometimes, environment for which we are writing script does not have csvkit isntalled and installing/compiling all might be overkill for just greping few lines of csv file.

1 Answers1

0
  • -1: require python2
  • +1: python2 is more commonon than csvkit

bash drop-in function:

function csvgrep(){
grep_criteria="$1"
matchcolumn="$2"
python2 -c "import csv, sys
rows = list(csv.reader(sys.stdin))
writer = csv.writer(sys.stdout)
writer.writerow(rows[0])
for row in rows[1:]:
  if row[${matchcolumn}] == '${grep_criteria}':
    writer.writerow(row)"
}

Usage:

csvgrep 'valueInLastColumn' '-1' < in.csv > out.csv