Shell Programming and Scripting

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Reading CSV file


👤 Login to reply

    #1  
Old 12-29-2017
as7951 as7951 is offline
Registered User
 
Reading CSV file

Hi experts,

Im having csv file with few columns which should contain data as shown below. Want to check if column 3 contain row with duplicate value(9876,9876) then corresponding to this in col2 should contain text "tax" and should not contain text "non".
Word "non" can come but if in column3 duplicate value are not present in rows, if a row contain only distinct value suppose 7623 then corresponding to this "non" can come in column 2.

Need to print error along with line number, if these condition are not met in file or col2 contain "non" against col3 contain duplicate value

CSV Data:

Code:
col1         col2   col3
inv	       tax	9876
inv	       tax	9876
inv	       non	7623
inv           tax    1234
inv           tax    1234

Bad Data :
will be considered incorrect if last row with col2 value as "non" and col 3 value as 1234
Code:
col1         col2   col3
inv	       tax	9876
inv	       tax	9876
inv	       non	7623
inv           tax    1234
inv           tax    1234
inv           non    1234


Last edited by Scrutinizer; 12-29-2017 at 04:36 AM.. Reason: code tags
Sponsored Links
    #2  
Old 12-29-2017
RudiC RudiC is online now Forum Staff  
Moderator
 
Not fully understanding what you're after, how about
Code:
awk '
                {++CNT[$3]
                }

$2 == "non"     {NON[$3] = NR
                }

NON[$3] &&
(CNT[$3] > 1)   {print "error ", NON[$3]
                }
' file
error  7

Sponsored Links
    #3  
Old 12-30-2017
as7951 as7951 is offline
Registered User
 
Hi Rudic,

With reference to above query, below is the objective i want to achieve looking into csv file.

Identify the rows with field(column-1) as INV and then on filtered list identify rows with identical values in the column-2 (document Number). In all such cases, check the value in field supplyType (column-3) and in that if any of the row has value TAX then in none of the other row should have value NON.

For ex-
If column three contain duplicate values then against those values in column2 we can have values as "tax","SEZ", "ISD", etc but not value as "non". If value in column 3 does not contain duplicate value(only single or unqiue value) in rows, then column 2 can contain value "non" against those distinct(single) value of colum3.

sample data :
Code:
col1         col2   col3
inv	       tax	9876
inv	       tax	9876
inv	       non	7623
inv           tax    1234
inv           tax    1234
inv           non    1234

here the last bundle of duplicate values of "1234"(column3) will be considered as wrong because in column 2 we are getting "non" against column 3(last row value 1234).
Instead it has to be "tax" at last row of column2.

For this error we need to print "error code" with line number.

---------- Post updated at 12:38 AM ---------- Previous update was at 12:14 AM ----------

Hi Rudic,

Thanks for the code,
will execute the same and will let you know for issues if any.
thank you once again

Last edited by Don Cragun; 12-30-2017 at 12:20 AM.. Reason: Add CODE tags again.
Sponsored Links
👤 Login to reply

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
Reading the value of particular column from csv file punpun66 Shell Programming and Scripting 3 11-30-2013 04:23 AM
Reading last line of a CSV file Chinky23 Shell Programming and Scripting 2 10-09-2012 04:23 PM
Reading from a CSV and writing in same CSV file kmajumder Shell Programming and Scripting 3 09-07-2012 06:11 PM
reading from a .csv file novice_user Shell Programming and Scripting 1 01-20-2009 07:13 AM
Script for reading .csv file creativeworld Shell Programming and Scripting 3 11-12-2007 01:47 AM



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

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?