---
title: "CSV parsing methods in Linux"
description: "#To get emails from CSV which is exported form your mail box grep -o -i ’[^,]@[^,]’ contacts.csv #Cut the required column."
url: "https://h3manth.com/scribe/csv-parsing-methods-linux/"
canonical_url: "https://h3manth.com/scribe/csv-parsing-methods-linux/"
markdown_url: "https://h3manth.com/scribe/blog/csv-parsing-methods-linux.md"
publishDate: "2009-10-22T00:00:00.000Z"
author: "Hemanth HM"
category: "linux"
tags: ["bash","linux"]
word_count: 216
reading_time_minutes: 2
x_markdown_tokens: 434
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# 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.

#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.
