Hi, I am newbie to perl, below is the script i have worked on which gives me exactly what i want. The only problem i am facing is speed. This script is mapping the AB,BA,IS,RT,ET from file1.csv and putting them into columns. file2.txt contains first column of file1.csv which i have cut using unix command (this i would like to avoid and map it from the script itself). my question- Is there any way I can make this script faster as file1.csv has 1.6 millions of lines and grep command goes through them 6 times which is making the script real slow (takes 6 hours to map). but does gives the output what i want.
Please help me
Thanks in advance.
John
file2.txt #has unique file1.csv column 1 data
output.txt
This is what i have,
[CODE=perl]$file1 = "file1.csv" ;
$file2 = "file2.txt" ;
open(FILE2,"<$f ile2");
while(<FILE2>) {
chomp $_;
$sc = $_;
$ab = `grep "$sc,AB," $file1`;
if($ab ne ""){
@array = split(/,/, $ab);
$ab_no = $array[2];
} else {
$ab_no = "\t";
}
$ba = `grep "$sc,BA,LAN " $file1`;
if($ba ne ""){
@array1 = split(/,/, $ba);
$barra = $array1[2];
} else {
$bar_a = "\t";
}
$is = `grep "$sc,IS,LA" $file1`;
if($is ne ""){
@array2 = split(/,/, $is);
$is_in = $array2[2];
} else {
$is_in = "\t";
}
$rt = `grep "$sc,RT,.*T O," $file1`;
if($rt ne ""){
@array3 = split(/,/, $rt);
$erto = $array3[2];
} else {
$erto = "\t";
}
$et = `grep "$sc,ET," $file1`;
if($et ne ""){
@array4 = split(/,/, $et);
$ex_tk = $array4[2];
} else {
$ex_tk = "\t";
}
print "$sc,$ab_no,$ba r_a,$is_in,$ert o,$ex_tk\n";
}
close(FILE1)[/CODE]
Please help me
Thanks in advance.
John
Code:
file1.csv 112311,AB,312342 112311,BA,LAN321 112311,IS,LA3423 321211,AB,342324 432342,BA,LAN322 432342,IS,LA3453 432342,RT,324343 432342,ET,ERTF
Code:
112311 321211 432342
Code:
sc,ab_no bar_a,is_in,erto,ex_tk 112311,312342,LAN321,LA3423, , 321211,342324, , , , , 432342, ,LAN322,LA3453,324343,ERTF
[CODE=perl]$file1 = "file1.csv" ;
$file2 = "file2.txt" ;
open(FILE2,"<$f ile2");
while(<FILE2>) {
chomp $_;
$sc = $_;
$ab = `grep "$sc,AB," $file1`;
if($ab ne ""){
@array = split(/,/, $ab);
$ab_no = $array[2];
} else {
$ab_no = "\t";
}
$ba = `grep "$sc,BA,LAN " $file1`;
if($ba ne ""){
@array1 = split(/,/, $ba);
$barra = $array1[2];
} else {
$bar_a = "\t";
}
$is = `grep "$sc,IS,LA" $file1`;
if($is ne ""){
@array2 = split(/,/, $is);
$is_in = $array2[2];
} else {
$is_in = "\t";
}
$rt = `grep "$sc,RT,.*T O," $file1`;
if($rt ne ""){
@array3 = split(/,/, $rt);
$erto = $array3[2];
} else {
$erto = "\t";
}
$et = `grep "$sc,ET," $file1`;
if($et ne ""){
@array4 = split(/,/, $et);
$ex_tk = $array4[2];
} else {
$ex_tk = "\t";
}
print "$sc,$ab_no,$ba r_a,$is_in,$ert o,$ex_tk\n";
}
close(FILE1)[/CODE]
Comment