Filter duplicate records from csv file with condition on one column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Filter duplicate records from csv file with condition on one column
# 1  
Old 12-28-2017
Filter duplicate records from csv file with condition on one column

I have csv file with 30, 40 columns
Pasting just three column for problem description
I want to filter record if column 1 matches CN or DN then,
check for values in column 2 if column contain 1235, 1235 then in column 3 values must be sequence of 2345, 2345
and if column 2 contains 6789, 6789 in row, then in column 3 values must be in sequence 7890, 7890
or if column 2 contains duplicate value(1234,1234) in row(1-4) in bundle, then column 3 must also contains duplicate value(4567,4567) in row(1-4)
or if column 2 contains duplicate value(5678,5678) in row(5-8) in bundle, then column 3 must also contains duplicate value(4321,4321) in row(5-8)
if combination as explained above is not present, then logs must be printed in another file with error code and line number

Sample file.
Code:
CN	1234	4567
CN	1234	4567
CN	1234	4567
CN	1234	4567
CN	5678	4321
CN	5678	4321
CN	5678	4321
CN	5678	4321


Last edited by jim mcnamara; 12-28-2017 at 12:25 PM..
# 2  
Old 12-28-2017
This is the kind of question that needs to have:
Sample good input that will not be "filtered"
Sample bad input -> expected output

Without this start we cannot help.

What code have you tried? Please show us where you are in your attempt.
# 3  
Old 12-28-2017
Hi Jim,

In this problem i want to look into csn file and need to print error if combination above does not exist in any row.(No changes to be done in csv file)
i tired the below code, but not sure what to do next,
Code:
awk '{if (x[$2$3]) { x_count[$2$3]++; print $0; if (x_count[$2$3] == 1) { print x[$2$3] } } x[$2$3] = $0}'

As asked by you :
Good input will be like below :
Code:
DT	DN	ON
CN	1234	4567
CN	1234	4567
CN	1234	4567
CN	1234	4567
CN	5678	4321
CN	5678	4321
CN	5678	4321
CN	5678	4321

Bad input will be like below Smiliemarked in red)
Code:
DT	DN    ON
CN	1234	4567
CN	1234	4567
CN	1234	4567
CN	5678	4564
CN	5678	4321
CN	5678	4564
CN	7890	7654
CN	7890	7654
CN	7890	3243


Last edited by Don Cragun; 12-29-2017 at 03:35 AM.. Reason: Add CODE tags again.
# 4  
Old 12-28-2017
And what should the output look like?
Do you always need 4 rows of identical values?
I can't recognize a pattern. How do we tell correct from wrong numbers?
Will it always be those exact numbers given in post#1?
# 5  
Old 12-28-2017
Taking a bit of a guess at the error message format but hopefully this is close enough for the OP to modify to their liking:

Code:
awk '
$2 in V && V[$2] != $3 {
    print "Line " NR " " $3 " <> " V[$2]
    next }
{ V[$2] = $3 }' inputfile

Output for testing data:
Code:
Line 5 4321 <> 4564
Line 9 3243 <> 7654

f you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.

Edit:

or another similar solution:

Code:
awk '
{ 
  if (V[$2] && V[$2] != $3 )
    print "Line " NR " " $3 " <> " V[$2]
  else V[$2] = $3
}' inputfile


Last edited by Chubler_XL; 12-28-2017 at 08:21 PM..
# 6  
Old 12-30-2017
Hi Rudic,

I don't want to modify input data in csv file and don't want output in diff file
i just want to throw/print error for the rows where condition is not met in csv file

File should contain data in two columns in below given format.
and numbers in row and column may vary.
In short if column 2 contain row(1-2) with duplicate values(1234,1234) and column 3 should also contain duplicate values(4567,4567) in row(1-2)
and false condition will be when column 2 contain duplicate value(0808,0808,0808) where in row(1-3) but column 3 does not contain duplicate value(4567,4567,1234) in rows(1-3) , where column 3 contain 1234 in row 3 which causes this condition to be false

hope im clear now
Good condition
Code:
DT	DN	ON
CN	1234	4567
CN	1234	4567
CN	9876	6543
CN	9876	6543
CN	5678	4321
CN	5678	4321
CN	0909	3089
CN	0909	3089

False condition in "red"
Code:
DT   DN     ON
CN   0808  4567
CN   0808  4567
CN   0808  1234

---------- Post updated at 03:31 AM ---------- Previous update was at 02:24 AM ----------

Hi chubler,

Could you please help me , how to execute these script.
As when i tried putting these code in .sh file then no output is coming
and when tried from command line getting syntax error at "next" command.

---------- Post updated 12-30-17 at 12:41 AM ---------- Previous update was 12-29-17 at 03:31 AM ----------

Hi chubler,

Thank you for the code, will run and test the same,
and will let you know for issue if any.

thanks Smilie

Last edited by Don Cragun; 12-29-2017 at 03:38 AM.. Reason: Add CODE tags again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Filtering records of a csv file based on a value of a column

Hi, I tried filtering the records in a csv file using "awk" command listed below. awk -F"~" '$4 ~ /Active/{print }' inputfile > outputfile The output always has all the entries. The same command worked for different users from one of the forum links. content of file I was... (3 Replies)
Discussion started by: sunilmudikonda
3 Replies

2. Shell Programming and Scripting

CSV File:Filter duplicate records from column1 & another column having unique record

Hi Experts, I have csv file with 30, 40 columns Pasting just 2 column for problem description. Need to print error if below combination is not present in file check for column-1 (DocumentNumber) and filter columns where value in DocumentNumber field is same. For all such rows, the field... (7 Replies)
Discussion started by: as7951
7 Replies

3. Shell Programming and Scripting

Filter file to remove duplicate values in first column

Hello, I have a script that is generating a tab delimited output file. num Name PCA_A1 PCA_A2 PCA_A3 0 compound_00 -3.5054 -1.1207 -2.4372 1 compound_01 -2.2641 0.4287 -1.6120 3 compound_03 -1.3053 1.8495 ... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

4. Shell Programming and Scripting

Identify duplicate values at first column in csv file

Input 1,ABCD,no 2,system,yes 3,ABCD,yes 4,XYZ,no 5,XYZ,yes 6,pc,noCode used to find duplicate with regard to 2nd column awk 'NR == 1 {p=$2; next} p == $2 { print "Line" NR "$2 is duplicated"} {p=$2}' FS="," ./input.csv Now is there a wise way to de-duplicate the entire line (remove... (4 Replies)
Discussion started by: deadyetagain
4 Replies

5. Linux

Filter a .CSV file based on the 5th column values

I have a .CSV file with the below format: "column 1","column 2","column 3","column 4","column 5","column 6","column 7","column 8","column 9","column 10 "12310","42324564756","a simple string with a , comma","string with or, without commas","string 1","USD","12","70%","08/01/2013",""... (2 Replies)
Discussion started by: dhruuv369
2 Replies

6. Shell Programming and Scripting

Removing duplicate records in a file based on single column explanation

I was reading this thread. It looks like a simpler way to say this is to only keep uniq lines based on field or column 1. https://www.unix.com/shell-programming-scripting/165717-removing-duplicate-records-file-based-single-column.html Can someone explain this command please? How are there no... (5 Replies)
Discussion started by: cokedude
5 Replies

7. Shell Programming and Scripting

Removing duplicate records in a file based on single column

Hi, I want to remove duplicate records including the first line based on column1. For example inputfile(filer.txt): ------------- 1,3000,5000 1,4000,6000 2,4000,600 2,5000,700 3,60000,4000 4,7000,7777 5,999,8888 expected output: ---------------- 3,60000,4000 4,7000,7777... (5 Replies)
Discussion started by: G.K.K
5 Replies

8. UNIX for Dummies Questions & Answers

CSV file:Find duplicates, save original and duplicate records in a new file

Hi Unix gurus, Maybe it is too much to ask for but please take a moment and help me out. A very humble request to you gurus. I'm new to Unix and I have started learning Unix. I have this project which is way to advanced for me. File format: CSV file File has four columns with no header... (8 Replies)
Discussion started by: arvindosu
8 Replies

9. Shell Programming and Scripting

Apply condition on fixed width file and filter records

Dear members.. I have a fixed width file. Requirement is as below:- 1. Scan each record from this fixed width file 2. Check for value under field no "6" equals to "ABC". If yes, then filter this record into the output file Please suggest a unix command to achieve this, my guess awk might... (6 Replies)
Discussion started by: sureshg_sampat
6 Replies

10. Shell Programming and Scripting

Find Duplicate records in first Column in File

Hi, Need to find a duplicate records on the first column, ANU4501710430989 0000000W20389390 ANU4501710430989 0000000W67065483 ANU4501130050520 0000000W80838713 ANU4501210170685 0000000W69246611... (3 Replies)
Discussion started by: Murugesh
3 Replies
Login or Register to Ask a Question