extract values from column with Perl

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract values from column with Perl
# 1  
Old 10-23-2009
extract values from column with Perl

Hi everybody
I have some problems with PERL programming.

I have a file with two columns, both with numeric values.
I have to extract the values > 50 from the 2nd columns and sum them among them.
The I have to sum the respective values in the first column on the same line and, at the end, I have to divide the sum of the first column values for the sum of the second column values > 50.

Can you help me? the script must be in Perl language.
Thank you very much,
Have a good weekend
# 2  
Old 10-24-2009
So as an example, you would have something like
1 20
3 40
7 60
8 90

The sum of the values in the second column which are larger than 50 is 60 + 90 = 150. The associated values in the first column are 7 + 8 = 15, so your answer is 15 / 150 = 0.1. Is that an accurate description of your problem?
# 3  
Old 10-24-2009
Oh yes, It's alright Figaro!
Could you help me with a Perl script?
# 4  
Old 10-24-2009
hi M.Elena,
here is the perl script which might help!!

while (<>)
  split(/ /);
 if ($_[1] > 50)
    $sum_col2 += $_[1] ;
    $sum_col1 += $_[0] ;
print("sum of col-2 is $sum_col2 \n");
print("sum of col-1 is $sum_col1 \n");


print("Answer is $rslt\n");

$ ./sol.pl ques 
sum of col-2 is 150 
sum of col-1 is 15 
Answer is 0.1

# 5  
Old 10-25-2009
I suppose "split(/ /);" does the delimiting? So if the delimiter is a semicolon (;), you would have "split(/;/);", correct?
# 6  
Old 10-25-2009
exactly !!!!!!
when u write 'split()' alone, by default it assigns the splitted fields in built-in array @_,
so u can access its contents as $_[0,1...].
# 7  
Old 10-25-2009
ok, it's perfect

Thank you very much not only for your help but, by your script, I understood something about Perl Language!

have a good day,
Maria Elena
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to extract values and print at end of each line

In the below perl I am trying to extract and print the values AF1=, the GT value, and F or QUAL diveded by 33 (rounded to the nearest whole #). The GT value is at the end after the GT:PL so all the possibilities are read into a hash h, then depending on the value that is in the line the... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Perl - Extract first column from file

Hi, I want to extract first column from a file and redirect the output to another file in perl. I am able to do this from command line by executing below commands. perl -anle 'print $F' Input.dat > Output.dat perl -ne '@F = split("\t", $_); print "$F\n";' Input.dat > Output.dat perl -anE... (7 Replies)
Discussion started by: Neethu
7 Replies

3. Shell Programming and Scripting

Extract values from a specific column to the end

Hello friends, I have a text file with many columns (no. columns vary from row to row) separated by space. I need to collect all the values from 18th column to the end from each line and group them as pairs and then numbering like below.. 1. 18th-col-value 19th-col-value 2. 20th-col-value ... (5 Replies)
Discussion started by: prvnrk
5 Replies

4. Shell Programming and Scripting

Perl regexp to extract first and second column

Hi, I am trying with the below Perl one-liner using regular expression to extract the first and second column of a text file: perl -p -e "s/\s*(\w+).*/$1/" perl -p -e "s/\s*.+\s(.+)\s*/$1\n/" whereas the text file's data looks like: Error: terminated 2233 Warning: reboot 3434 Warning:... (3 Replies)
Discussion started by: royalibrahim
3 Replies

5. Shell Programming and Scripting

Extract values from Perl variable

Hi Guys, I am stuck in a problem. I have a variable in Perl script which has value for example X=a-b-c; Now, I want to extract a b c separately into different 3 variables. I know this can be done in shell using awk but Perl behaves a bit different. Can anybody help me on this please?... (3 Replies)
Discussion started by: prashant2507198
3 Replies

6. Shell Programming and Scripting

Extract first column from second line in perl

Hello Gurus I have a source file which has the first line as header and the rest are the records I need to extract the first column from the second line to extract a value I/P ... (7 Replies)
Discussion started by: Pratik4891
7 Replies

7. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

Hello, I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2 SDF4 -0.211654 SDF4 0.978068 ... (1 Reply)
Discussion started by: Diya123
1 Replies

8. UNIX for Dummies Questions & Answers

How to extract one column from csv file in perl?

Hi everyone, i am new to perl programming, i have a problem in extracting single column from csv file. the column is the 20th column, please help me.. at present i use this code #!C:/perl/bin use warnings; use strict; my $file1 = $ARGV; open FILE1, "<$file1" or die "Can't... (13 Replies)
Discussion started by: kvth
13 Replies

9. Shell Programming and Scripting

perl-extract data from hash values

Hello, I have parsed an xml file using perl to get the hash values and the output looks like this $VAR1 = { 'RT' => { 'List' => { 'String' => ... (1 Reply)
Discussion started by: userscript
1 Replies

10. Shell Programming and Scripting

I need to extract last column of a file and compare the values

Hi, I am new to unix and I need help in solving below mentioned issue, really appreciate ur help. I have a file sam, john, 2324, 07142007 tom, thomson, 2343, 07142007 john, scott, 2478, 07142007 its a comma delimited file, I need to extract the last column from each line and this... (4 Replies)
Discussion started by: vukkusila
4 Replies
Login or Register to Ask a Question