Perl script to find particular field and sum it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script to find particular field and sum it
# 8  
Old 03-16-2010
Quote:
Originally Posted by thillai_selvan
What error you are getting?
it is keep on saying cant open: no such file when i call script on my file

but both files are there and both have execute permission
# 9  
Old 03-16-2010
Code:
use strict;
use warnings;

open FH, "<inp" or die "Can't open file : $!\n";
my @data=<FH>;
my @result;
my @count;
my $word;
foreach (@data )
{
        push(@result,split);
}
for ( my $i=4; $i <= $#result; $i=$i+5 )
{
        push(@count,$result[$i]);
}

my %count_hash;
shift @count;
foreach $word (@count)
{
        unless (defined($count_hash{$word}) )
        {
                $count_hash{$word} = 0;

        }
       $count_hash{$word}=$count_hash{$word}+1;
}

foreach my $word (keys %count_hash)
{
                print $word," Comes for ",$count_hash{$word}," times\n";
}

Use the above code.
Here the file inp is input file.
This input file is having your input data.
Thats why you are getting error. May be your input file name is different.
So correct it.
# 10  
Old 03-16-2010
Quote:
Originally Posted by thillai_selvan
Code:
use strict;
use warnings;

open FH, "<inp" or die "Can't open file : $!\n";
my @data=<FH>;
my @result;
my @count;
my $word;
foreach (@data )
{
        push(@result,split);
}
for ( my $i=4; $i <= $#result; $i=$i+5 )
{
        push(@count,$result[$i]);
}

my %count_hash;
shift @count;
foreach $word (@count)
{
        unless (defined($count_hash{$word}) )
        {
                $count_hash{$word} = 0;

        }
       $count_hash{$word}=$count_hash{$word}+1;
}

foreach my $word (keys %count_hash)
{
                print $word," Comes for ",$count_hash{$word}," times\n";
}

Use the above code.
Here the file inp is input file.
This input file is having your input data.
Thats why you are getting error. May be your input file name is different.
So correct it.

Yes thanks, i was using wrong format .pl instead of .txt.

Now one more question arise, here we knew that we r doint 4th column but how will you do if you have many colums and you want the last one. like in awk i thin we just put $ sign at the end. here in this script what we will chang to get the last field, no matter which column number it is

Thanks for teaching
# 11  
Old 03-16-2010
Simple!!! . $#array will have the last index of the array. So using this we can easily get the last column number.
Example:
Code:
print $#data; #this will give 5.

So we can easily identify that totally there are 5 columns are there
Index starts from 0. So $data[4] represents the last column value.

Last edited by thillai_selvan; 03-16-2010 at 06:05 AM..
# 12  
Old 03-16-2010
Quote:
Originally Posted by thillai_selvan
Simple. $#array will have the last index of the array. So using this we can easily get the last column number.
Example:
Code:
print $#data; #this will give 5.

So we can easily identify that totally there are 5 columns are there
Index starts from 0. So $data[4] represents the last column value.

I understand this, but what i am saying is..let's imagine you have more than more than 2000 colums ( just imagine )..and you don't want to count all of them 0 to 1999. so what will you do so that the script just take the last field without specifying the index number
# 13  
Old 03-16-2010
Code:
print $array_name[-1]

This will give the last element in that array.
Code:
print $array_name[-2]

This will give the 2nd last element in that array.
Are you expecting this?
# 14  
Old 03-16-2010
Quote:
Originally Posted by thillai_selvan
Code:
print $array_name[-1]

This will give the last element in that array.
Code:
print $array_name[-2]

This will give the 2nd last element in that array.
Are you expecting this?
Yes yes thanks for teaching all this.. thank you

---------- Post updated at 08:22 AM ---------- Previous update was at 05:13 AM ----------

Quote:
Originally Posted by thillai_selvan
Use the following code
Code:
open FH, "<inp" or die "Can't open file : $!\n";
my @data=<FH>;
my @result;
my @count;
foreach (@data )
{
    push(@result,split);
}
for ( my $i=4; $i <= $#result; $i=$i+5 )
{
    push(@count,$result[$i]);
}

my %count_hash;
shift @count; #to remove the e from the array
foreach my $word (@count)
{
       $count_hash{$word}=$count_hash{$word}+1;
}

foreach my $word (keys %count_hash)
{
            print $word," Comes for ",$count_hash{$word}," times\n";
}

The output I am getting is follows

Code:
6 Comes for 1 times
4 Comes for 1 times
3 Comes for 1 times
2 Comes for 2 times

can you please tell why you did +5
Code:
for ( my $i=4; $i <= $#result; $i=$i+5 )

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to find out the sum of output on the screen

Hi, I am new to scripting.I am using redhat linux 6.I am just finding a way to summing up the output displayed as below.It will help me a lot since we used to get this kind of output every now and then. I am pasting here the output .What is required is this o/p needs to be summed up and... (3 Replies)
Discussion started by: muraliinfy04
3 Replies

2. Homework & Coursework Questions

Calculate sum of the field

Hi All, I need to calculat the sum of the particular field in text file. And the datatype of the field in file is decimal(18,2). If the file is small, then I am facing any problem. But the file is huge, then the result is converted into exponential format. I tried using various command thr... (0 Replies)
Discussion started by: lathanandhini
0 Replies

3. Shell Programming and Scripting

Find repeated word and take sum of the second field to it ,for all the repeated words in awk

Hi below is the input file, i need to find repeated words and sum up the values of it which is second field from the repeated work.Im trying but getting no where close to it.Kindly give me a hint on how to go about it Input fruits,apple,20,fruits,mango,20,veg,carrot,12,veg,raddish,30... (11 Replies)
Discussion started by: 100bees
11 Replies

4. Shell Programming and Scripting

Shell script to find the sum of argument passed to the script

I want to make a script which takes the number of argument, add those argument and gives output to the user, but I am not getting through... Script that i am using is below : #!/bin/bash sum=0 for i in $@ do sum=$sum+$1 echo $sum shift done I am executing the script as... (3 Replies)
Discussion started by: mukulverma2408
3 Replies

5. Shell Programming and Scripting

To find sum & average of 8th field

Hi Friends, I have many files like below. total,0.7%,0.0%,0.2%,0.0%,0.2%,0.7%,98.0% total,1.9%,0.0%,0.4%,0.0%,0.0%,6.8%,90.6% total,0.9%,0.0%,0.4%,0.0%,0.0%,0.0%,98.5% total,1.4%,0.0%,0.7%,0.0%,0.2%,2.9%,94.5% total,0.7%,0.0%,0.4%,0.0%,0.0%,0.9%,97.7%... (13 Replies)
Discussion started by: SunilB2011
13 Replies

6. Shell Programming and Scripting

perl sum 2nd field in an array

Hi Everyone, ($total+=$_) for @record; assume @record=(1,2,3), so the result is 6. if @record=("1 3","2 3","3 3"), would like to sum up the 2nd field of this array, the result is 9. i tried " ($total+=$) for @record ", cannot, please advice. Thanks ---------- Post updated at 03:45... (1 Reply)
Discussion started by: jimmy_y
1 Replies

7. Homework & Coursework Questions

Help with shell script to find sum of first n numbers of Fibonacci series

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Shell script to find sum of first n numbers of Fibonacci series 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: Kshitija
0 Replies

8. Shell Programming and Scripting

Shell script to find the sum of first n Fibonacci numbers

pls give me the solution for this i need it for my exam pls pls pls Shell script to find the sum of first n Fibonacci numbers (1 Reply)
Discussion started by: Kshitija
1 Replies

9. Shell Programming and Scripting

How to sum particular field in text file?

how to sum particular field in text file i am having text file like 222|4000|abc 333|5000|xyz 444|6000|mno i want sum of second field i.e 4000+5000+6000 can u pls help (3 Replies)
Discussion started by: suryanarayana
3 Replies

10. Shell Programming and Scripting

Sum value from selected lines script (awk,perl)

Hello. I face this (2 side) problem. Some lines with this structure. ........... 12345678 4 12345989 13 12346356 205 12346644 74 12346819 22 ......... The first field (timestamp) is growing (or at least equal). 1)Sum the second fields if the first_field/500 are... (8 Replies)
Discussion started by: paolfili
8 Replies
Login or Register to Ask a Question