CSV parsing methods in Linux

To get emails from CSV which is exported form your mail box

grep -o -i '[^,]@[^,]' contacts.csv

Cut the required column assuming delimiter to be ","

cut -d, -f2,3 < input.csv

For given index range

grep ${VALUE} input.csv | cut -d, -f${INDEX}

Get first two columns of the cvs file

awk '{print $1"\t"$2}' input.csv

CSV which is of the regex /^(.*),,/ $f is the file here

awk -F,'{ if ($1 != "") prefix=$1; else printf "%s%s\n", prefix,$0 }' $f

CSV of the format field1 , field2 , field3 , field4

we can set the FS to a regexp as FS=’^ *| *, *|

FS=','

for(i=1;i<=NF;i++){ gsub(/^ *| *$/,"",$i); print "Field " i " is " $i; }

For CSV of the format "field1","field2","field3","field4"

we can set the FS to FS=’^”|”,”|”$’ or FS=’”,”|”‘

FS=','

for(i=1;i<=NF;i++){ gsub(/^ *"|" *$/,"",$i); print "Field " i " is " $i; }

For CSV => field1,"field2,with,commas",field3 ,"field4,foo"

$0=$0",";
while($0) { match($0,/[^,],| *"[^"]" *,/); sf=f=substr($0,RSTART,RLENGTH); # save gsub(/^ *"?|"? *,$/,"",f); # remove extra print "Field" ++c " is " f; sub(sf,""); # that which is matched }

This is my personal collection. Most of them suggested by the uber cool g33ks of terminal. Few bits patched by me.Out there are many ways please do comment them here.

Share this