Check to identify duplicate values at first column in csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check to identify duplicate values at first column in csv file
# 1  
Old 04-25-2013
Check to identify duplicate values at first column in csv file

Hello experts,

I have a requirement where I have to implement two checks on a csv file:

1. Check to see if the value in first column is duplicate, if any value is duplicate script should exit.

2. Check to verify if the value at second column is between "yes" or "no", if it is anything else script should exit.

My input file looks like:
hiring,no
system,yes
hiring,yes
quota,no

OS is solaris.

I have been trying to implement/list my first requirement using awk but without any success, i tried this but there is no output:

Code:
awk 'x[$1]++ == 1 { print $1 " is duplicated"}' FILENAME

awk'x[$1]++FS=","



is not working either, since above file has hiring at two places script should come out.

Please advise.
# 2  
Old 04-25-2013
You need to exit after the print.
# 3  
Old 04-25-2013
Code:
$ cat input
hiring,no
system,yes
hiring,yes
quota,no
quota,maybe

Code:
$ sort input | awk 'NR == 1 {p=$1; next} p == $1 { print $1 " is duplicated"} {p=$1}' FS=","
hiring is duplicated
quota is duplicated

Code:
$ awk '$2 != "yes" && $2 != "no" { print $2 " on line " NR " is not yes/no"}' FS="," input
maybe on line 5 is not yes/no

In a shell script, save the output from each awk command to a file, and use [ -s file ] to determine whether to exit the script or not.
These 2 Users Gave Thanks to hanson44 For This Post:
# 4  
Old 04-25-2013
Perform pre-increment and check if greater than 1 to identify duplicates:
Code:
awk -F, ' ++A[$1] > 1 { print $1 "is duplicate"; exit 1 } ' file

This User Gave Thanks to Yoda For This Post:
# 5  
Old 05-09-2013
Thank you to both of you hanson44 and Yoda, I used both the utilities in my script and they are working absolutely file. Thank you again.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: as7951
5 Replies

3. Shell Programming and Scripting

Find duplicate values in specific column and delete all the duplicate values

Dear folks I have a map file of around 54K lines and some of the values in the second column have the same value and I want to find them and delete all of the same values. I looked over duplicate commands but my case is not to keep one of the duplicate values. I want to remove all of the same... (4 Replies)
Discussion started by: sajmar
4 Replies

4. Shell Programming and Scripting

Remove duplicate values in a column(not in the file)

Hi Gurus, I have a file(weblog) as below abc|xyz|123|agentcode=sample code abcdeeess,agentcode=sample code abcdeeess,agentcode=sample code abcdeeess|agentadd=abcd stereet 23343,agentadd=abcd stereet 23343 sss|wwq|999|agentcode=sample1 code wqwdeeess,gentcode=sample1 code... (4 Replies)
Discussion started by: ratheeshjulk
4 Replies

5. 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

6. 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

7. Shell Programming and Scripting

Remove the values from a certain column without deleting the Column name in a .CSV file

(14 Replies)
Discussion started by: dhruuv369
14 Replies

8. Shell Programming and Scripting

Fetching values in CSV file based on column name

input.csv: Field1,Field2,Field3,Field4,Field4 abc ,123 ,xyz ,000 ,pqr mno ,123 ,dfr ,111 ,bbb output: Field2,Field4 123 ,000 123 ,111 how to fetch the values of Field4 where Field2='123' I don't want to fetch the values based on column position. Instead want to... (10 Replies)
Discussion started by: bharathbangalor
10 Replies

9. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies
Login or Register to Ask a Question