Remove subsequent duplicate only


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove subsequent duplicate only
# 1  
Old 02-22-2013
Remove subsequent duplicate only

Hi,

I've been trying to dig myself out of this, but nothing has worked out yet.

I have an input like this:

Code:
1-Num1
1-Num2
2-Num3
3-Num4
1-Num5
3-Num11
2-Num11
1-Num13
1-Num16
3-Num18
4-Num19
2-Num20
1-Num22
3-Num23
1-Num24

From this, I want to remove duplicates, not all, but only those that are just above the repeated value. In other words, retain the second repetition, but only if it follows the first occurrence. I want to run this comparison ignoring the values before -, but retaining them in the results.

Someone please help me out with this.

Thanks!
# 2  
Old 02-22-2013
Not sure I understand. Pls post desired output and the logics how it's derived.
# 3  
Old 02-22-2013
Hi,

Thanks for responding, here is a simplified case. Say I have this as input
Code:
1-num1
2-num2
3-num2
4-num3
5-num3
2-num2

Now what I want to do is not just find repetitions and remove them, but to find repetitions that are only in the next line and remove the first occurrence of that value. Repetitions are checked on $2 with FS as "-". So the output should be

Code:
1-num1
3-num2
4-num3
5-num3
2-num2

.
# 4  
Old 02-22-2013
Here is a not so elegant approach:
Code:
awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' filename | awk '!a[$1]++' | awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }'

This User Gave Thanks to Yoda For This Post:
# 5  
Old 02-22-2013
Ask sed to set a branch target, quit at EOF, read next line into buffer, if identical, reduce and go to target print the first line, remove it and branch to target.
Code:
sed '
:loop
$q
N
s/^\(.*\)\n\1$/\1/
t loop
P
s/.*\n//
t loop
'

# 6  
Old 02-22-2013
Hi Jamie,
check this out:

Code:
awk '!d[$0]++' file


Code:
1-num1
2-num2
3-num2
4-num3
5-num3

# 7  
Old 02-22-2013
Quote:
Originally Posted by rveri
Hi Jamie,
check this out:
Code:
awk '!d[$0]++' file

jamie_123 wants to remove first occurrence of the duplicate, not the second. Your code with remove the second and subsequent occurrences.

This is why we have to reverse the lines of the file first, then remove the duplicate and finally reverse the lines back.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Remove duplicate

Hi, How can I replace || with space and then remove duplicate from following text? T111||T222||T444||T222||T555 Thanks in advance (10 Replies)
Discussion started by: tinku981
10 Replies

2. Shell Programming and Scripting

Remove duplicate

Hi , I have a pipe seperated file repo.psv where i need to remove duplicates based on the 1st column only. Can anyone help with a Unix script ? Input: 15277105||Common Stick|ESHR||Common Stock|CYRO AB 15277105||Common Stick|ESHR||Common Stock|CYRO AB 16111278||Common Stick|ESHR||Common... (12 Replies)
Discussion started by: samrat dutta
12 Replies

3. Shell Programming and Scripting

How to remove duplicate ID's?

HI I have file contains 1000'f of duplicate id's with (upper and lower first character) as below i/p: a411532A411532a508661A508661c411532C411532 Requirement: But i need to ignore lowercase id's and need only below id's o/p: A411532 A508661 C411532 (9 Replies)
Discussion started by: buzzme
9 Replies

4. Shell Programming and Scripting

Remove Duplicate Records

Hi frinds, Need your help. item , color ,desc ==== ======= ==== 1,red ,abc 1,red , a b c 2,blue,x 3,black,y 4,brown,xv 4,brown,x v 4,brown, x v I have to elemnet the duplicate rows on the basis of item. the final out put will be 1,red ,abc (6 Replies)
Discussion started by: imipsita.rath
6 Replies

5. Shell Programming and Scripting

Remove duplicate records

I want to remove the records based on duplicate. I want to remove if two or more records exists with combination fields. Those records should not come once also file abc.txt ABC;123;XYB;HELLO; ABC;123;HKL;HELLO; CDE;123;LLKJ;HELLO; ABC;123;LSDK;HELLO; CDF;344;SLK;TEST key fields are... (7 Replies)
Discussion started by: svenkatareddy
7 Replies

6. Shell Programming and Scripting

remove duplicate

Hi, I am tryung to use shell or perl to remove duplicate characters for example , if I have " I love google" it will become I love ggle" or even "I loveggle" if removing duplicate white space Thanks CC (6 Replies)
Discussion started by: ccp
6 Replies

7. Shell Programming and Scripting

how to remove duplicate lines

I have following file content (3 fields each line): 23 888 10.0.0.1 dfh 787 10.0.0.2 dssf dgfas 10.0.0.3 dsgas dg 10.0.0.4 df dasa 10.0.0.5 df dag 10.0.0.5 dfd dfdas 10.0.0.5 dfd dfd 10.0.0.6 daf nfd 10.0.0.6 ... as can be seen, that the third field is ip address and sorted. but... (3 Replies)
Discussion started by: fredao
3 Replies

8. Shell Programming and Scripting

Remove duplicate

Hi all, I have a text file fileA.txt DXRV|02/28/2006 11:36:49.049|SAC||||CDxAcct=2420991350 DXRV|02/28/2006 11:37:06.404|SAC||||CDxAcct=6070970034 DXRV|02/28/2006 11:37:25.740|SAC||||CDxAcct=2420991350 DXRV|02/28/2006 11:38:32.633|SAC||||CDxAcct=6070970034 DXRV|02/28/2006... (2 Replies)
Discussion started by: sabercats
2 Replies

9. Shell Programming and Scripting

Remove duplicate ???

Hi all, I have a out.log file CARR|02/26/2006 10:58:30.107|CDxAcct=1405157051 CARR|02/26/2006 11:11:30.107|CDxAcct=1405157051 CARR|02/26/2006 11:18:30.107|CDxAcct=7659579782 CARR|02/26/2006 11:28:30.107|CDxAcct=9534922327 CARR|02/26/2006 11:38:30.107|CDxAcct=9534922327 CARR|02/26/2006... (3 Replies)
Discussion started by: sabercats
3 Replies

10. Shell Programming and Scripting

remove duplicate

i have a text its contain many record, but its written in one line, i want to remove from that line the duplicate record, not record have fixed width ex: width = 4 inputfile test.txt =abc cdf abc abc cdf fgh fgh abc abc i want the outputfile =abc cdf fgh only those records can any one help... (4 Replies)
Discussion started by: kazanoova2
4 Replies
Login or Register to Ask a Question