DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Perl (http://www.daniweb.com/forums/forum112.html)
-   -   Duplicate lines in a text file (http://www.daniweb.com/forums/thread199759.html)

kummetha Jun 25th, 2009 1:22 pm
Duplicate lines in a text file
 
How to print the duplicates in a text file. Also the line number where the duplicate text has been found needs to be printed. pls help

KZ

msvinaykumar Jun 25th, 2009 1:42 pm
Re: Duplicate lines in a text file
 
Hi,

If you are working in Unix then, Use following command

<code>
cat -n file-name > file_name2
</code>

Then use
<code>
sort -u -k 2,4 file_name2
</code>

kummetha Jun 25th, 2009 1:45 pm
Re: Duplicate lines in a text file
 
No, I am not working in Unix. I am working with Windows.
KZ

msvinaykumar Jun 25th, 2009 1:48 pm
Re: Duplicate lines in a text file
 
Or use Uniq command

<code>
$ cat happybirthday.txt

Happy Birthday to You!

Happy Birthday to You!

Happy Birthday Dear vinay!

Happy Birthday to You!
</code>

<code>
$ sort happybirthday.txt | uniq -dc
3 Happy Birthday to You!
</code>

KevinADC Jun 25th, 2009 4:22 pm
Re: Duplicate lines in a text file
 
Quote:

Originally Posted by kummetha (Post 899974)
How to print the duplicates in a text file. Also the line number where the duplicate text has been found needs to be printed. pls help

KZ

What have you tried? Where are you stuck?

kummetha Jun 25th, 2009 4:52 pm
Re: Duplicate lines in a text file
 
many a thanks msvinaykumar, i am looking for a perl script that works with windows.
KZ

Prakash_8111 Jun 26th, 2009 5:14 am
Re: Duplicate lines in a text file
 
open(FILE,"1.txt") || die "$!"; ## 1.txt is a file name
%seen =();
$line=0 ;
while (<FILE>)
{
$seen{$_}++;
$line++;
print "$line : $_" if $seen{$_} > 1 ;
}

kummetha Jun 26th, 2009 1:40 pm
Re: Duplicate lines in a text file
 
thanks prakash, it did really work.
also if you could provide help for a script that reads a blank line in a txt file and prints the line above the blank line.
Eg
abcdef

william
marc
sumeet

nair

output:
abcdef
sumeet
thanks in advance

KevinADC Jun 26th, 2009 2:45 pm
Re: Duplicate lines in a text file
 
At some point you should start doing your own school work and the rest of the people participating in this thread you stop doing it for you.


All times are GMT -4. The time now is 6:40 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC