Delete duplicate lines... with a twist!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete duplicate lines... with a twist!
# 1  
Old 11-22-2011
Java Delete duplicate lines... with a twist!

Hi, I'm sorry I'm no coder so I came here, counting on your free time and good will to beg for spoonfeeding some good code. I'll try to be quick and concise!

Got file with 50k lines like this:
Code:
"Heh, heh. Those darn ninjas. They're _____."*wacky
The "canebrake", "timber" & "pygmy" are types of what?*rattlesnakes
Science : The second space shuttle was named ------*challenger

Problem is that somewhere (anywhere) in file may appear a similar line (but usually not exactly the same), which needs to be recognized as duplicate and deleted!

My example - of what could be found and should be recognized (and deleted) as duplicate:
Code:
the 'canebrake', 'timber' & 'pygmy' are types of what*rattleSNAKES
SCIENCE::: the;second;space;shuttle;was;named ??????*challenger

So I guess algorithm should basically do this:

1. from each line read only letters [a-z], [A-Z] and numbers [0-9] and disregard any possible spacing or special characters or punctuation

2. compare with every other line (in same manner a-Z, 0-9) and if same arrangement of letters and numbers is found (ignoring spacing, case, special chars...) delete one of the lines (doesn't matter which one)


Scripting language doesn't matter... perl, python, ruby, vi, awk, sed... anything goes =) (using archlinux box)

Much appreaciated!
# 2  
Old 11-22-2011
Code:
awk '{s=tolower($0);gsub("[^a-z]","",s);x[s]=$0} END {for(i in x) print x[i]}' file

This User Gave Thanks to shamrock For This Post:
# 3  
Old 11-22-2011
Thanks, it worked.

But slight observation: I had some 200 lines in file that would differentiate only by numbers and this code would (incorrectly) count them as duplicate.
# 4  
Old 11-23-2011
Quote:
Originally Posted by shadowww
Thanks, it worked.

But slight observation: I had some 200 lines in file that would differentiate only by numbers and this code would (incorrectly) count them as duplicate.
Not sure what you mean...can you post a sample of how that input file looks like...
# 5  
Old 11-23-2011
Code:
$
$ cat f42
"Heh, heh. Those darn ninjas. They're _____."*wacky
The "canebrake", "timber" & "pygmy" are types of what?*rattlesnakes
Science : The second space shuttle was named ------*challenger
the quick brown 123 fox jumps over the lazy ?@! dog
the 456 quick brown fox jumps over the ~*%# lazy dog
123 the quick brown @%#$!^ fox jumps over the lazy ~()& dog
$
$
$
$ perl -lne '$h=$_; s/[^\w]|_//g; tr/A-Z/a-z/; s/(.)(?=.*?\1)//g;
             $_=join "",sort split "";
             print $h if not defined $x{$_}; $x{$_}++
            ' f42
"Heh, heh. Those darn ninjas. They're _____."*wacky
The "canebrake", "timber" & "pygmy" are types of what?*rattlesnakes
Science : The second space shuttle was named ------*challenger
the quick brown 123 fox jumps over the lazy ?@! dog
the 456 quick brown fox jumps over the ~*%# lazy dog
$
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 6  
Old 11-23-2011
Quote:
Originally Posted by shamrock
Not sure what you mean...can you post a sample of how that input file looks like...
Sure, It is 5mb compilation of trivia questions. One question per row with * for separator from answer (file will be used by irc trivia bot). Aim is to weed out automatically as much duplicate questions as possible. There is sample in my first post but here is bigger chunk of file: www.pastebin.com/u1a1ZGHr which also shows entries that get selected as duplicates and deleted with your code - these are the ones starting with "Algebra : "


thx, tyler_durden, will try this perl code in moment

edit:
tyler_durden's perl code shrunk questions from 55983 lines to 20915
shamrock's awk code shrunk questions from 55983 lines to 40724

I have yet to compare in detail (manually? :<) but I think perl code ate too much 'duplicates'. Can't believe its more then half, but don't know yet, I may be wrong, have to confirm.

Last edited by shadowww; 11-23-2011 at 01:31 PM..
# 7  
Old 11-23-2011
Quote:
Originally Posted by shadowww
Sure, It is 5mb compilation of trivia questions. One question per row with * for separator from answer (file will be used by irc trivia bot). Aim is to weed out automatically as much duplicate questions as possible. There is sample in my first post but here is bigger chunk of file: sample trivia - Pastebin.com which also shows entries that get selected as duplicates and deleted with your code - these are the ones starting with "Algebra : "
Is * the only non alphanumeric character in the input file as that makes it easy...but is that really the case as your original post had others...so if you define it clearly a better awk solution can be given...
This User Gave Thanks to shamrock For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete duplicate like pattern lines

Hi I need to delete duplicate like pattern lines from a text file containing 2 duplicates only (one being subset of the other) using sed or awk preferably. Input: FM:Chicago:Development FM:Chicago:Development:Score SR:Cary:Testing:Testcases PM:Newyork:Scripting PM:Newyork:Scripting:Audit... (6 Replies)
Discussion started by: tech_frk
6 Replies

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

3. Shell Programming and Scripting

Delete duplicate rows

Hi, This is a followup to my earlier post him mno klm 20 76 . + . klm_mango unix_00000001; alp fdc klm 123 456 . + . klm_mango unix_0000103; her tkr klm 415 439 . + . klm_mango unix_00001043; abc tvr klm 20 76 . + . klm_mango unix_00000001; abc def klm 83 84 . + . klm_mango... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

4. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

5. UNIX for Advanced & Expert Users

In a huge file, Delete duplicate lines leaving unique lines

Hi All, I have a very huge file (4GB) which has duplicate lines. I want to delete duplicate lines leaving unique lines. Sort, uniq, awk '!x++' are not working as its running out of buffer space. I dont know if this works : I want to read each line of the File in a For Loop, and want to... (16 Replies)
Discussion started by: krishnix
16 Replies

6. UNIX for Dummies Questions & Answers

How to delete partial duplicate lines unix

hi :) I need to delete partial duplicate lines I have this in a file sihp8027,/opt/cf20,1980182 sihp8027,/opt/oracle/10gRelIIcd,155200016 sihp8027,/opt/oracle/10gRelIIcd,155200176 sihp8027,/var/opt/ERP,10376312 and need to leave it like this: sihp8027,/opt/cf20,1980182... (2 Replies)
Discussion started by: C|KiLLeR|S
2 Replies

7. UNIX for Dummies Questions & Answers

Delete lines with duplicate strings based on date

Hey all, a relative bash/script newbie trying solve a problem. I've got a text file with lots of lines that I've been able to clean up and format with awk/sed/cut, but now I'd like to remove the lines with duplicate usernames based on time stamp. Here's what the data looks like 2007-11-03... (3 Replies)
Discussion started by: mattv
3 Replies

8. UNIX for Dummies Questions & Answers

How to delete or remove duplicate lines in a file

Hi please help me how to remove duplicate lines in any file. I have a file having huge number of lines. i want to remove selected lines in it. And also if there exists duplicate lines, I want to delete the rest & just keep one of them. Please help me with any unix commands or even fortran... (7 Replies)
Discussion started by: reva
7 Replies

9. UNIX for Dummies Questions & Answers

Delete duplicate lines and print to file

OK, I have read several things on how to do this, but can't make it work. I am writing this to a vi file then calling it as an awk script. So I need to search a file for duplicate lines, delete duplicate lines, then write the result to another file, say /home/accountant/files/docs/nodup ... (2 Replies)
Discussion started by: bfurlong
2 Replies

10. Shell Programming and Scripting

delete semi-duplicate lines from file?

Ok here's what I'm trying to do. I need to get a listing of all the mountpoints on a system into a file, which is easy enough, just using something like "mount | awk '{print $1}'" However, on a couple of systems, they have some mount points looking like this: /stage /stand /usr /MFPIS... (2 Replies)
Discussion started by: paqman
2 Replies
Login or Register to Ask a Question