Dear Monk,
Let me at the risk of being repetitive since you've already gotten advise on this subject try and make
things more clear to you.
Consider the following code:
#!/usr/bin/perl -w use strict; my %storage=(); # untested, but should in theory work... my $junk=<DATA>; #get rid of header while(my $line=<DATA>){ chomp($line); # Get rid of newline my ($host,$dist_id,$status)=split(/[\s\n\t]+/,$line); # split on + any whitespace my $storage{$host) = { host=> $host, dist_id => $dist_id, status=> $status }; # Put this into a hash keyed on the two fields we +want to key on } # We never removed the new line character from $junk so... print $junk; # We reclaim this from the trash can foreach my $key(sort keys %storage){ # # Print the remaining record matching the keys printf "%s\t%s\t%s\n",$storage{$key}->{host},$storage{$key}->{dis +t_id},$storage{$key}->{status}; } exit(0); __END__ COMPUTER DISTRIBUTION_ID STATUS 30F-WKS `1781183799.xxxx1' IC--- 30F-WKS `1781183799.xxx11' IC--- ADM34A3F9 `1781183799.41455' IC---
The way this works is you are going to overwrite subsequent records that you read in for the same host in the hash %storage and as a result get the last record in your data set output. Since you said in CB you don't know if your fields are space or tab separated I covered both bases by using the regex /[\s\t\n]+/ in the split callout.
Hope this helps
In reply to Re: File Manipulation - Need Advise!
by blue_cowdawg
in thread File Manipulation - Need Advise!
by nashkab
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |