Script for extracting data from csv file based on column values.

Hi all,

I am new to shell script.I need your help to write a shell script.
I need to write a shell script to extract data from a .csv file where columns are ',' separated.
The file has 5 columns having values say column 1,column 2.....column 5 as below along with their valuesm.
ref_num,Account Number,Misc, error 1,error 2

COD  , 121, misc, 0, 0
COD1, 123, misc2,1, 1

Now i want to write a script to first check if the file name.csv exists at /home/home1.
If the file exists i want to display column 1 to column 5 header along with their values only if the column error 1 or error 2 column has any other value than 0 i.e it should not have 0.

Output for above input should be like below:

ref_num:COD1
Account Number:123
Misc:misc2
error 1:1
error 2:1

so what did you try? Here are hints.

-f FILENAME can be used to identify if the file is exist or not.

awk can be used to get the report.

I tried below but seems some problem.

awk -F, '(NR==1){h1=$1;h2=$2;h3=$3;h4=$4;h5=$5;next} ($4||$5){print h1":"$1"\n"h2":"$2"\n"h3":"$3"\n"h4":"$4"\n"h5":"$5"\n"}' file
#!/usr/bin/bash
FILE=/home/home1/name.csv
if [ ! -f $FILE ] ; then
  echo " file $FILE is not exist"
  exit
fi

awk -F , 'int($4)!="0"||int($5)!="0" {printf "ref_num:%s\nAccount Number:%s\nMisc:%s\nerror 1:%s\nerror2:%s\n",$1,$2,$3,$4,$5}' $FILE