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!

Parsing query 1

Status
Not open for further replies.

rabinski

Programmer
May 13, 2005
21
GB
Hi gurus -

I have an input file like so

10.0.0.1 servername
10.0.0.2 servername2
10.0.0.2 servername2
10.0.0.3 servername3

I am reading in the file and just writing out the Servername to a new output file -

But - I only want one entry for servername2 (and any other dupes) in the new file ...

Any pointers would be great

Thanks in advance

Rab

 
If you're on *nix (I'm not sure how STDOUT redirection works on Win32):
Code:
 perl -ane 'print "$F[0]\n" unless ($seen{$F[1]}++)' input_filename > output_filename
 
use a hash:-

Code:
[b]#!/usr/bin/perl[/b]

while (<DATA>) {
  chomp;
  ($ip, $server) = split(/ /);
  $servers{"$server"} = 1;
}

while (($key, $value) = each %servers) {
  print "$key\n";
}

__DATA__
10.0.0.1 servername
10.0.0.2 servername2
10.0.0.2 servername2
10.0.0.3 servername3


Kind Regards
Duncan
 
Dunc -

You are indeed a dude - cheers mate all sorted ....

Rab
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top