File 1 has ranges 3-9, 2-6 etc
3 9
2 6
12 20
File2 has values: column 1 indicates the range and column 2 has values.
1 4
2 4
3 5
4 4
5 4
6 1
7 1
8 1
9 4
I would like to calculate the sum of values (file2, column2) for ranges in file1). Eg: If range is 3-9, then sum of values will be 5+4+4+1+1+1+4 = 20
Below is the script for doing the same:
use strict;
use warnings;
use feature qw( say );
use List::Util qw( sum );
my $file1 = 'file1.txt';
my $file2 = 'file2.txt';
my @file2;
{
open(my $fh, '<', $file2)
or die "Can't open $file2: $!\n";
while (<$fh>) {
my ($k, $v) = split;
$file2[$k] = $v;
}
}
{
open(my $fh, '<', $file1)
or die "Can't open $file1: $!\n";
while (<$fh>) {
my ($start, $end) = split;
say sum grep defined, @file2[$start .. $end];
}
}
Friday, February 22, 2013
Monday, February 18, 2013
Capitalize every first letter of a word and others to lower case
How can you capitalize(Only first character) each word in a line using perl and change remaining to lower case?
let say there is a file:
let say there is a file:
> cat temp
hI thIs is woRld
How can we make it hI thIs is woRld
Hi This Is World
Its very simple to do it in perl:
perl -pe 's/(\w+)/\u\L$1/g' your_file
If you want to do it inplace just add a -i flag:
perl -pi -e 's/(\w+)/\u\L$1/g' your_file
Wednesday, February 13, 2013
Print the next power of 2 for a given number
Below is the program that I have written for printing the next power of 2 for a given number.
The code mostly uses the bitwise operations in c/c++.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
int main ()
{
int num;
scanf("%d",&num);
int count=0;
if(!((num != 1) && (num&(num-1))))
{
printf("%d is already a power of two.\n",num);
printf("%d is next number power of 2.\n",num*2);
}
else
{
while((num>>=1)&&(++count));
printf("%0.0f",pow(2.0,count+1));
}
}
The code mostly uses the bitwise operations in c/c++.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
int main ()
{
int num;
scanf("%d",&num);
int count=0;
if(!((num != 1) && (num&(num-1))))
{
printf("%d is already a power of two.\n",num);
printf("%d is next number power of 2.\n",num*2);
}
else
{
while((num>>=1)&&(++count));
printf("%0.0f",pow(2.0,count+1));
}
}
Thursday, February 7, 2013
Sorting files based upon the numeric value before the first field separator
Lets say i have a list of files.
for eg:
ABCD5_Avgsdfgsg.txt
ABCD7_ff.txt
CD8_PKG_CV.txt
ABCD1_11.txt
BCD2_fbefjf.txt
D3_C.txt
ABCD4_123.txt
ABCD10_123.txt
I want to sort these files according to the first number before the first underscore.
I tried with sort but with no proper solution then i tried it in perl and this worked.Hopefully the below helps.
ls -1 | perl -F"_" -lane '$F[0]=~s/[a-zA-Z]//g;$X{$F[0]}=$_;END{foreach(sort {$a<=>$b} keys(%X)){print $X{$_}}}'
tested below:
> ls -1
ABCD10_123.txt
ABCD1_11.txt
ABCD4_123.txt
ABCD5_Avgsdfgsg.txt
ABCD7_ff.txt
BCD2_fbefjf.txt
CD8_PKG_CV.txt
D3_C.txt
> ls -1 | perl -F"_" -lane '$F[0]=~s/[a-zA-Z]//g;$X{$F[0]}=$_;END{foreach(sort {$a<=>$b} keys(%X)){print $X{$_}}}'
ABCD1_11.txt
BCD2_fbefjf.txt
D3_C.txt
ABCD4_123.txt
ABCD5_Avgsdfgsg.txt
ABCD7_ff.txt
CD8_PKG_CV.txt
ABCD10_123.txt
>
for eg:
ABCD5_Avgsdfgsg.txt
ABCD7_ff.txt
CD8_PKG_CV.txt
ABCD1_11.txt
BCD2_fbefjf.txt
D3_C.txt
ABCD4_123.txt
ABCD10_123.txt
I want to sort these files according to the first number before the first underscore.
I tried with sort but with no proper solution then i tried it in perl and this worked.Hopefully the below helps.
ls -1 | perl -F"_" -lane '$F[0]=~s/[a-zA-Z]//g;$X{$F[0]}=$_;END{foreach(sort {$a<=>$b} keys(%X)){print $X{$_}}}'
tested below:
> ls -1
ABCD10_123.txt
ABCD1_11.txt
ABCD4_123.txt
ABCD5_Avgsdfgsg.txt
ABCD7_ff.txt
BCD2_fbefjf.txt
CD8_PKG_CV.txt
D3_C.txt
> ls -1 | perl -F"_" -lane '$F[0]=~s/[a-zA-Z]//g;$X{$F[0]}=$_;END{foreach(sort {$a<=>$b} keys(%X)){print $X{$_}}}'
ABCD1_11.txt
BCD2_fbefjf.txt
D3_C.txt
ABCD4_123.txt
ABCD5_Avgsdfgsg.txt
ABCD7_ff.txt
CD8_PKG_CV.txt
ABCD10_123.txt
>
Subscribe to:
Posts (Atom)