Home Man
Search
Today's Posts
Register

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

Tags
awk array sequential rows pattern, linux, perl, shell script, shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 12-29-2017
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
# 2  
Old 12-29-2017
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

# 3  
Old 12-30-2017
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.
Login to Reply

« Previous Thread | Next 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
Reading Xml file and print the values into the text file in columnwise? sravanreddy UNIX for Dummies Questions & Answers 4 07-23-2015 03:13 AM
Reading XML file and print the values in the text file using Linux shell script sravanreddy UNIX for Dummies Questions & Answers 1 07-22-2015 11:26 AM
reading a file extracting information writing to a file Bruble Shell Programming and Scripting 8 10-12-2011 12:28 PM
Reading UNIX commands from file and redirecting output to a file rahulparo Shell Programming and Scripting 8 07-13-2011 08:09 AM
fatal: cannot open file `TNAME' for reading (No such file or directory) kunwar Shell Programming and Scripting 1 03-11-2011 07:03 PM
Searching for Log / Bad file and Reading and writing to a flat file mlpathir Shell Programming and Scripting 3 10-03-2010 06:01 PM
Reading a file and sending mail based on content of the file nimu1979 UNIX for Advanced & Expert Users 6 01-07-2009 03:23 AM
Reading file names from a file and executing the relative file from shell script anushilrai Shell Programming and Scripting 4 03-10-2006 04:25 AM
Reading specific contents from a file and appending it to another file dnicky Shell Programming and Scripting 5 10-04-2005 05:45 AM


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

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