The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
unique sort contents of a variable praveenbvarrier Shell Programming and Scripting 2 05-20-2008 04:12 AM
how to sort, and count unique data all at once? amatuer_lee_3 Shell Programming and Scripting 13 05-15-2008 07:48 AM
sort function in perl DILEEP410 Shell Programming and Scripting 2 09-14-2007 05:03 AM
sort and uniq in perl reggiej Shell Programming and Scripting 4 05-18-2006 07:46 PM
Sort file in perl annececile Shell Programming and Scripting 4 06-21-2002 05:52 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #8  
Old 02-08-2008
Registered User
 

Join Date: Feb 2007
Posts: 38
Quote:
Originally Posted by matrixmadhan View Post
read the file
split the record
use the third field
populate in a hash => this would maintain uniqueness
when displaying use sort keys %hash
Thanks for your input. Iam new to perl is it possible to give one simple example pls.
Reply With Quote
Forum Sponsor
  #9  
Old 02-08-2008
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
sample code and file
try this

Code:
>cat b
1|2|3
4|9|4
3|1|2
Code:
>cat b.pl
#! /opt/third-party/bin/perl

open(FILE, "<", $ARGV[0]);

while(<FILE>) {
  chomp;
  my @arr = split(/\|/);
  $fileHash{$arr[1]}++;
}

close(FILE);

foreach my $k ( sort keys %fileHash ) {
  print "$k\n";
}

exit 0
Reply With Quote
  #10  
Old 02-08-2008
Registered User
 

Join Date: Nov 2007
Posts: 85
cut -d'|' f3 file_name | sort -u
Reply With Quote
  #11  
Old 02-08-2008
Registered User
 

Join Date: Feb 2006
Posts: 17
Sort and Unique in Perl

Almost same as MatrixMadhan's. Row count was not included, so the following code is just for completeness:

Code:
#!/usr/bin/perl -w
use strict;

# Program to get unique values for 3rd column and print them

open(FILE, "b.txt");

my %list = ();

while(<FILE>){
   chomp;
   my @array = split(/\|/);
   $list{$array[2]}++;
}

close(FILE);

# Print out the results

foreach my $value (sort keys %list) {
   print "The unique values are $value\n";
}

print "Number of rows are ".keys(%list);
Reply With Quote
  #12  
Old 02-08-2008
Registered User
 

Join Date: Jan 2008
Posts: 328
Good examples already, this is just a more compact form of the same thing:

Code:
#!/usr/bin/perl
use warnings;
use strict;
unless ($ARGV[0]) {
    die "Usage: perl scriptname.pl filename";
}
my %list = ();
while(<>){
   $list{(split(/\|/))[2]}++;
}
print "$_ = $list{$_}\n" for (keys %list);
exit(0);
Uses perls optimized filehandling and no temp variables so should be fast and efficient.
Reply With Quote
  #13  
Old 02-08-2008
Registered User
 

Join Date: Feb 2007
Posts: 38
Thanks every one.
Thanks MatrixMadhan and MobileUser!!
Iam able to make use of the same code.
Thanks KevinADC, Just that, the count is comming for each entry of hash. I would like to have one count at the end. So that I can write the unique keys to a file and Count to a seperate header file.

Thanks again.
Reply With Quote
  #14  
Old 02-08-2008
Registered User
 

Join Date: Feb 2007
Posts: 38
Iam using lot of other packages in the script. So when I try to use it gives me an error.

PHP Code:
Global symbol "%list" requires explicit package name at b.pl line 19 
How to declare the hash variable in the script prior to declaration?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0