Thursday, February 18, 2010

Read a csv file from unix shell script

The following script can be used to read the contents of a csv file.

The csv file contains the following data

100,TOM,Manager
200,Rachel,CEO

The script will read all the columns by considering the field separator as comma.I've specified the IFS as ','.

/************Start of the scipt **********************/
function readfile {


FILENAME="$1"

exec 8<$FILENAME

while read -u8 line

do

IFS=','

set $line

echo $2

done

}

readfile emp.csv
 
/************End  of the scipt **********************/
 
The function readfile will accept a input parameter file name and it read the second column in the file. the script should be customized according to the requirements
 
 

2 comments:

Anonymous said...

You did an impressive job by posting this. Explained Thoroughly and perfectly.

Unix Training in Chennai | Unix course in Chennai

farsiya said...

Digital Marketing Services in Medavakkam
https://smedigital.in/digital-marketing-company-in-medavakkam.php

SME Digital is your trusted partner for digital marketing services in Medavakkam. They specialize in helping small and medium-sized enterprises boost their online presence, drive targeted traffic, and achieve measurable results. Their dedicated team of experts offers tailored solutions, including SEO, social media management, pay-per-click advertising, and website optimization, designed to propel your business forward in the digital age. With SME Digital, you can harness the power of the internet to reach a wider audience and thrive in today's competitive market.

Powered By Blogger