0

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

4
Contributors
8
Replies
9
Views
8 Years
Discussion Span
Last Post by KevinADC
0

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>

0

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

0

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>

0

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?

0

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

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

Edited by Reverend Jim: Fixed formatting

0

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

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.