Csv file parsing and validating


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Csv file parsing and validating
# 1  
Old 04-15-2014
Csv file parsing and validating

Hi,
I have basic knowledge on unix shell scripting(not an expert).

My requirement is reading the csv file using the schema defined in the configuration file and if the condition is not mached then move the unmatched record to a error file and matched good records into other file.

In brief:
Here i'm defining the schema of an input file in normal text file and I'm calling it as configuration file. My configuration file looks like :
col1 integer NN
col2 string
col3 string NN

Also I'm having a data file ( say its a .csv file) and it looks like :

id,name,location
1,John,FL
2,Merry,,
3,Taylor,CA
a,George,MI


So here i need to write unix sript such a way that while reading the csv data file ,it should refer to the configuration file for the datatype and whether that field is NULL or not. If it is not satisfying the condition it should drop that whole record and move for the next row for the validation check.

Here it has to read the 1st row value from the csv file and check the datatype of that value from the configuration file. If the value type of the 1st row is matched with the datatype of the 1st column , then it has to move the matched record into a new file and if unmatched record is found it has to move those error records into an error file.
How this can be done using UNIX shell scripting.

Here my expected result is :

id,name,age
1,John,FL
3,Taylor,CA


Rest of the records are dropped because :

2,Merry,, ---> here 3rd filed is NULL but in configuration file it is NOT NULL(NN)

a,George,MI --> here 1st field value is STRING where in conf file it is INTEGER.

So how this could be achieved in unix script and what are the commands need to be used to do this.

Please let me know if the scenario is not clear.

Thanks,
Shree11
# 2  
Old 04-15-2014
You only listed two conditionals but hopefully you can take the syntax of this script and modify it to your needs.
Code:
#!/bin/bash

> BADFILE
> GOODFILE

cat sourcefile | awk -F, '
  BEGIN { badfile = "BADFILE"; goodfile = "GOODFILE" }
  {
    if($1 !~ /^[0-9]*$/)print $0 >> badfile
    else if($3 == "")print $0 >> badfile
    else print $0 >> goodfile
  }'


Last edited by Franklin52; 04-18-2014 at 10:41 AM.. Reason: Please use code tags
# 3  
Old 04-15-2014
try also:
Code:
awk -F, '{x=$1; if (((x+=0) && ($3)) || NR==1) {of="good"} else {of="bad"} ; print > of}' in

# 4  
Old 04-16-2014
I will let you know what i have tried.
I created one script file called validate.sh
Code:
#!/bin/bash

awk -F"|" ' 
BEGIN {
getline var < "configFile"
getline var < "configFile"
n = split( var , arr , "|" )
for ( i = 1 ; i <= n ; ++i )
{
        if( arr[i] == "Numeric" )
                regexp[i] = "^[0-9][0-9]*$"
        if( arr[i] == "String" )
                regexp[i] = "^[a-zA-Z0-9][a-z[A-Z0-9]*$"
}
}
{
for( i = 1 ; i <= NF ; ++i )
{
        if ( ! match ( $i , regexp[i] ) )
                break;
        if ( i == NF )
                print $0
}
}' dataFile

Here the above code is to validate the datatype. Eventhough in my configuration file if i have only 4 columns and in datafile if i have 5 columns, then 5th column is also coming as output which is not expected.

Here my congiguration file is :
col1|col2|col3|col4
Numeric|String|String|Numeric


And Data file is
col1|col2|col3|col4
510|abc|xxx|450
510|abc11|yyy|350
510|pqr|zz11z|670
312|22a2|439|110
312|qqq|rrr|11a0
3$33|sss|bbbb|110
333|A11ss|aAa|110


Also here i have to check for the NULL and field length conditions.
how it can be done?

Last edited by shree11; 04-18-2014 at 08:03 AM..
# 5  
Old 04-18-2014
How my above code changes for the below configuration file(configFile) .

col1,Numeric
col2,String
col3,String
col4,Numeric

Please help me on this.
# 6  
Old 04-18-2014
If both schjema file (conf) and data file (file) as comma separated
Below code will do the job and send eligible records to a file "goodrec" and bad data to "badrec"

Code:
awk -F "," 'NR == FNR {a[NR] = $2; next}
  FNR == 1 {print $0 > "goodrec"; print $0 > "badrec"}
  FNR > 1 {for(i = 1; i <= NF; i++)
  {if((a[i] == "integer" && ($i + 0) == $i) || (a[i] == "string" && $i != "")) {f = 1} else {f = 0};
  if(f == 0) {print $0 > "badrec"; next}} print $0 > "goodrec"}' conf file

# 7  
Old 04-18-2014
Hi SriniSho,

The code suggested by you, we I'm passing the config file?

Does the code taking both the data file and config file into account?

When i do the below , I'm not getting the proper output in the good and bad files:
Code:
awk -F "," 'NR == FNR {a[NR] = $2; next}   FNR == 1 {print $0 > "goodrec"; print $0 > "badrec"}   FNR > 1 {for(i = 1; i <= NF; i++)   {if((a[i] == "integer" && ($i + 0) == $i) || (a[i] == "string" && $i != "")) {f = 1} else {f = 0};   if(f == 0) {print $0 > "badrec"; next}} print $0 > "goodrec"}' dataFile


Tahnks,
Shree
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Parsing a CSV File

Hello All, I have an input CSV file like below, where first row data can be in different position after every run of the tool, i.e. pzTest in below example is in column 1, but it can be also in 3 column and same for all the headers in the first row. pzTest, pzExtract, pxUpdate, pzInfo... (1 Reply)
Discussion started by: asirohi
1 Replies

2. Shell Programming and Scripting

Parsing csv file and pass to a variable

Hi, Newbie here and I need some help to parse a csv file that contains fields separated by ",". What I need to achieve here is, read the 1 line file and extract 240 fields and pass to a variable and then read the next 240 fields and pass to a variable, over and over. If anyone can assist that... (4 Replies)
Discussion started by: tmslixx
4 Replies

3. Shell Programming and Scripting

Help required in parsing a csv file

Hi Members, I am stuck with the following problem. Request your kind help I have an csv file which contains, 1 header record, data records and 1 footer record. Sample is as below Contents of cm_update_file_101010.csv -------------------------------------------------- ... (6 Replies)
Discussion started by: ramakanth_burra
6 Replies

4. Shell Programming and Scripting

Parsing a CSV file and deleting all rows on condition

Hello list, I am working on a csv file which contains two fields per record which contain IP addresses. What I am trying to do is find records which have identical fields(IP addresses) which occur 4(four) times, and if they do, delete all records with that specific identical field(ip address). ... (4 Replies)
Discussion started by: landossa
4 Replies

5. Shell Programming and Scripting

Parsing complicated CSV file with sed

Yes, there is a great doc out there that discusses parsing csv files with sed, and this topic has been covered before but not enough to answer my question (unix.com forums). I'm trying to parse a CSV file that has optional quotes like the following: "Apple","Apples, are fun",3.60,4.4,"I... (3 Replies)
Discussion started by: analog999
3 Replies

6. Shell Programming and Scripting

Parsing a CSV File

Hey guys, I'm in the process of learning PHP and BASH scripting. I'm getting there, slowly ;) I would like some help with parsing a CSV file. This file contains a list of hostnames, dates, and either Valid, Expired, or Expired Soon in the last column. Basically, I want to parse the file,... (12 Replies)
Discussion started by: dzl
12 Replies

7. Shell Programming and Scripting

2 problems: Mailing CSV file / parsing CSV for display

I have been trying to find a good solution for this seemingly simple task for 2 days, and I'm giving up and posting a thread. I hope someone can help me out! I'm on HPUX, using sqlplus, mailx, awk, have some other tools available, but can't install stuff that isn't already in place (without a... (6 Replies)
Discussion started by: soldstatic
6 Replies

8. Shell Programming and Scripting

CSV file parsing and validation

I have a CSV file that needs to through two seperate processes (in the end there will be 2 files (Dload.unl and Tload.unl and we'll say the input file name is mass.csv). I have a processfile() function that will call the process Dload funtion. In Dload I want to read mass.csv into Dload and then... (1 Reply)
Discussion started by: dolo21taf
1 Replies

9. Shell Programming and Scripting

Parsing a csv file

I am trying to parse a csv file in the below 'name-value pair' format and then use the values corresponding to the name. Type:G,Instance:instance1,FunctionalID:funcid,Env:dev,AppName:appname... (6 Replies)
Discussion started by: chiru_h
6 Replies

10. Shell Programming and Scripting

Help in parsing a CSV file with Shell script

I have a CSV file which contains number series as one of the fields. Some of the records of the field look like : 079661/3 I have to convert the above series as 079661 079662 079663 and store it as 3 different records. Looking for help on how to achieve this. Am a newbie at Shell... (10 Replies)
Discussion started by: mihirk
10 Replies
Login or Register to Ask a Question