Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Non unique Duplicate entries deletion. Sorry bad subject in last post 1

Status
Not open for further replies.

samdaman

Technical User
Oct 23, 2002
23
NZ
samdaman (TechnicalUser) May 16, 2003
Hi. I am a newbie (as you will all see in a moment).

I have a text file example

Dialer129
Dialer195
Dialer303
Dialer145
Dialer201
Dialer145;;0239028;;10;;10;;8

And I need to do the following

If a dialer number appears twice, delete the one that does not have any extra information, so as in the above file

Delete Dialer145
Keep Dialer145;;0239028;;10;;10;;8
Keep all other unique entries like DIaler129 etc.
I must Keep Dialer as it is used in a command line of the router for later.

Here is what I have. I have taken some from a thread off this site, and also from other FAQS. Whilst I have read some (loads of) FAQS, I have been unable to understand them well enough to amend them to my needs. And in the 3 weeks that I have started Perl, I have written some perl scripts to automate cisco router processes, which I was amazed to see work.
Thanks for the use or your knowledge, I hope to add it to my own

Cheers

Sam

open DATA, &quot;<cismultilink.txt&quot;;
$Dialer = Dialer149;
$Multi = &quot;Dialer145;;0239028;;10;;10;;8&quot;;
@array = ($Dialer, $Multi);
while (<DATA>) {
close DATA;
}
map { $seen{$_}++ } @array;
my @dupes = grep { $seen{$_} > 1 } keys %seen;
print;

close DATA;

__END__
 
Code:
use strict;
use warnings;

open DATA, &quot;<cismultilink.txt&quot;;

my %pruned;

while(<DATA>)
{
	chomp; #eat the trailing newline
	if(/^(Dialer\d+)(.*)$/) #check if it's a line starting with Dialer and numbers
	{
		#set the hash at key &quot;Dialer###&quot; to the extended info, if any, if
		#the hash key doesn't exist or the length of the extended info of
		#the new entry is longer than the stored one's
		$pruned{$1} = $2 if(!exists $pruned{$1} || (length $pruned{$1} < length $2));
	}
}

close DATA;

for(keys %pruned)
{
	#print the original data (likely in not the same order)
	#with the shorter, duplicate-key entries removed
	print &quot;$_$pruned{$_}\n&quot;;
}

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Thanks ICRF it worked like a treat

If ur still trying to see the light, i guess I havent left the dark ages yet.

Thanks again

Sam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top