Find and replace duplicate column values in a row


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace duplicate column values in a row
# 1  
Old 04-15-2010
Find and replace duplicate column values in a row

I have file which as 12 columns and values like this

Code:
 
1,2,3,4,5
a,b,c,d,e
b,c,a,e,f
a,b,e,a,h

if you see the first column has duplicate values, I need to identify (print it to console) the duplicate value (which is 'a') and also remove duplicate values like below. I could be in two different scripts that fine.

Code:
 
1,2,3,4,5
a,b,c,d,e
b,c,a,e,f
,,e,a,h

Appreciate your help !

Last edited by zaxxon; 04-15-2010 at 10:47 AM.. Reason: code tags please
# 2  
Old 04-15-2010
AWK version

Code:
awk -F, '{for (i=1;i<=NF;i++){if (v[i,$i]++){$i=""}};print $0}' OFS=","

# 3  
Old 04-15-2010
Thanks for the reply kcoder.

Its not printing out the duplicate value
# 4  
Old 04-16-2010
Code:
while(<DATA>){
	chomp;
	my @tmp = split(",",$_);
	for(my $i=0;$i<=$#tmp;$i++){
		if(exists $hash{$i}->{$tmp[$i]}){
			print "," if $i!=$#tmp;
		}
		else{
			print $tmp[$i];
			print "," if $i!=$#tmp;
			$hash{$i}->{$tmp[$i]}=1;
		}
	}
	print "\n";
}
__DATA__
1,2,3,4,5
a,b,c,d,e
b,c,a,e,e
a,b,e,a,h

# 5  
Old 04-16-2010
It works, seems to be identifying duplicates overall. But I need to identify duplicates per column.
# 6  
Old 04-16-2010
A slight modification to the post suggested by Kcoder24

Code:
 
awk -F, '{for (i=1;i<=NF;i++){if (v[i,$i]++){b[$i]=$i;$i=""}};print $0}  END { print "dups are" ;for ( i in b) print i}' OFS=","  input_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find lines with duplicate values in a particular column

I have a file with 5 columns. I want to pull out all records where the value in column 4 is not unique. For example in the sample below, I would want it to print out all lines except for the last two. 40991764 2419 724 47182 Cand A 40992936 3591 724 47182 Cand B 40993016 3671 724 47182 Cand C... (5 Replies)
Discussion started by: kaktus
5 Replies

2. Shell Programming and Scripting

Do replace operation and awk to sum multiple columns if another column has duplicate values

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (12 Replies)
Discussion started by: as7951
12 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. Programming

How to add one to each row values and keep it after the value in the column?

Dear Folks Hello I have a column of numbers, say: 26 79 68 I want to add one to each row value and get this desire column: 26 27 79 80 68 69 (6 Replies)
Discussion started by: sajmar
6 Replies

5. UNIX for Dummies Questions & Answers

awk to sum column field from duplicate row/lines

Hello, I am new to Linux environment , I working on Linux script which should send auto email based on the specific condition from log file. Below is the sample log file Name m/c usage abc xxx 10 abc xxx 20 abc xxx 5 xyz ... (6 Replies)
Discussion started by: asjaiswal
6 Replies

6. Shell Programming and Scripting

Print every 5 4th column values as separate row with different first column

Hi, I have the following file, chr1 100 200 20 chr1 201 300 22 chr1 220 345 23 chr1 230 456 33.5 chr1 243 567 90 chr1 345 600 20 chr1 430 619 21.78 chr1 870 910 112.3 chr1 914 920 12 chr1 930 999 13 My output would be peak1 20 22 23 33.5 90 peak2 20 21.78 112.3 12 13 Here the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

7. Shell Programming and Scripting

duplicate row based on single column

I am a newbie to shell scripting .. I have a .csv file. It has 1000 some rows and about 7 columns... but before I insert this data to a table I have to parse it and clean it ..basing on the value of the first column..which a string of phone number type... example below.. column 1 ... (2 Replies)
Discussion started by: mitr
2 Replies

8. Shell Programming and Scripting

Converting values in a ROW to COLUMN

Hi All, I needd to convert values in a row to a column. eg: Input is as: value1,value2,value3,value4,.........,value N Required Output: Value1 Value2 Value3 . . . Value N Please help.... (3 Replies)
Discussion started by: sambaman
3 Replies

9. Shell Programming and Scripting

Convert column values into row

hi, I have a requirement where in I read the values from a file using awk. The resulting data should be converted into row format from column format. For ex: My log file login.lst contains the following SERVER1 DB1 SERVER2 DB2 SERVER3 DB3 SERVER4 DB4 I use awk to grep only the server... (6 Replies)
Discussion started by: senthil3d
6 Replies

10. Shell Programming and Scripting

Delete a row that has a duplicate column

I'm trying to remove lines of data that contain duplicate data in a specific column. For example. apple 12345 apple 54321 apple 14234 orange 55656 orange 88989 orange 99898 I only want to see apple 12345 orange 55656 How would i go about doing this? (5 Replies)
Discussion started by: spartan22
5 Replies
Login or Register to Ask a Question