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

Recommended Answers

All 8 Replies

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>

No, I am not working in Unix. I am working with Windows.
KZ

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>

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?

many a thanks msvinaykumar, i am looking for a perl script that works with windows.
KZ

open(FILE,"1.txt") || die "$!";  ## 1.txt is a file name 
%seen =();
$line=0 ;
while (<FILE>)
{
        $seen{$_}++;
        $line++;
        print "$line : $_"  if $seen{$_} > 1 ;
}

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.