![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#8
|
|||
|
|||
|
Thanks for your input. Iam new to perl is it possible to give one simple example pls.
|
| Forum Sponsor | ||
|
|
|
#9
|
|||
|
|||
|
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
|
|
#10
|
|||
|
|||
|
cut -d'|' f3 file_name | sort -u
|
|
#11
|
|||
|
|||
|
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);
|
|
#12
|
|||
|
|||
|
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);
|
|
#13
|
|||
|
|||
|
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. |
|
#14
|
|||
|
|||
|
Iam using lot of other packages in the script. So when I try to use it gives me an error.
PHP Code:
|
|||
| Google The UNIX and Linux Forums |