Shell script to extract data from csv file based on certain conditions | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Shell script to extract data from csv file based on certain conditions

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-28-2013
Vivekit82 Vivekit82 is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 28 March 2014, 4:24 PM EDT
Posts: 62
Thanks: 2
Thanked 0 Times in 0 Posts
Shell script to extract data from csv file based on certain conditions

Hi Guys,

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



Code:
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 error 1 or error 2 column has any other value than 0 i.e it should not have 0.


Output should be like below:


Code:
ref_num:COD1
Account Number:123
Misc:misc
error 1:1
error 2:1

the above output should be send to mail abc@gmail.com
Sponsored Links
    #2  
Old 08-28-2013
krishmaths krishmaths is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 30 June 2014, 4:32 AM EDT
Location: Chennai, India
Posts: 443
Thanks: 22
Thanked 70 Times in 68 Posts
To display record if either of the error code is non-zero:

Code:
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


To display record if both the error codes are non-zero:

Code:
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

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script to extract data from csv file Vivekit82 UNIX for Dummies Questions & Answers 7 08-28-2013 04:25 AM
Shell Script to Dynamically Extract file content based on Parameters from a pdf file DIps Shell Programming and Scripting 3 05-08-2013 01:31 PM
extract data in a csv file based on a certain field. GroveTuckey Shell Programming and Scripting 9 11-02-2011 10:17 PM
Extract file records based on some field conditions mehimadri Shell Programming and Scripting 9 11-30-2010 12:22 PM
Exporting data as a CSV file from Unix shell script appu2176 Shell Programming and Scripting 2 04-25-2010 07:38 AM



All times are GMT -4. The time now is 07:28 AM.