Hi!
I have 4 columns of tab-delimited data. column 1 has two patterns, 'UA' and 'DWN'. I want to select all the rows where coulmn 4 has a value above zero. From these, print rows where column 1 is 'DWN' and print all the 'UA' rows when there are 3 or more UA's between 'DWN'.
I am stuck here and any help is appreciated. No, this is not class work please.
Thanks in advance
sample.txt
UA 16X 936154 4.313130105
DWN 16X 936199 3.584936089
DWN 16X 936259 -4.087834994
DWN 16X 936309 0.930493862
DWN 16X 936374 4.212535002
DWN 16X 936429 3.327646506
DWN 16X 936474 -2.916253542
UA 16X 936649 3.171373127
UA 16X 936754 2.445098511
UA 16X 936809 4.638831704
UA 16X 936864 3.212792628
UA 16X 936914 3.403498281
UA 16X 940084 -3.683286264
UA 16X 940134 3.949742106
DWN 16X 940244 5.031138006
DWN 16X 940309 3.814187618
UA 16X 940364 2.589227288
UA 16X 940409 3.427800184
DWN 16X 940909 3.582933058
DWN 16X 940959 3.493582633
UA 16X 941014 3.664841581
UA 16X 941124 3.321131006
UA 16X 941184 3.784367884
outfile.txt [what i expect]
DWN 16X 936199 3.584936089
DWN 16X 936309 0.930493862
DWN 16X 936374 4.212535002
DWN 16X 936429 3.327646506
UA 16X 936649 3.171373127
UA 16X 936754 2.445098511
UA 16X 936809 4.638831704
UA 16X 936864 3.212792628
UA 16X 936914 3.403498281
UA 16X 940134 3.949742106
DWN 16X 940244 5.031138006
DWN 16X 940309 3.814187618
DWN 16X 940909 3.582933058
DWN 16X 940959 3.493582633
UA 16X 941014 3.664841581
UA 16X 941124 3.321131006
UA 16X 941184 3.784367884
here is my code, which runs, but does not capture all the rows with 'UA' with a positive value.
#!/usr/bin/perl -w
use strict;
use diagnostics;
open(FILER1, "sample.txt") || die "couldn't open the file\n";
open(FILEW1, ">outfile.txt") || die "couldn't create the file\n";
my ($line, $all, $count, $full, $flag, $num);
my (@all);
while(<FILER1>){
$line=<FILER1>;
split /\t/, $line;
if($_[3] >= 0) {
if($line=~/DWN/) {
print FILEW1 "$line";
$flag =1;
}
else {
push(@all, $line);
$count= scalar @all;
$full = join '', @all;
if ($count >=3 && $flag ==1) {
$flag =0;
print FILEW1 "$full";
$count=0;
$full = '';
@all = '';
$all='';
}
}
}
}
I have 4 columns of tab-delimited data. column 1 has two patterns, 'UA' and 'DWN'. I want to select all the rows where coulmn 4 has a value above zero. From these, print rows where column 1 is 'DWN' and print all the 'UA' rows when there are 3 or more UA's between 'DWN'.
I am stuck here and any help is appreciated. No, this is not class work please.
Thanks in advance
sample.txt
UA 16X 936154 4.313130105
DWN 16X 936199 3.584936089
DWN 16X 936259 -4.087834994
DWN 16X 936309 0.930493862
DWN 16X 936374 4.212535002
DWN 16X 936429 3.327646506
DWN 16X 936474 -2.916253542
UA 16X 936649 3.171373127
UA 16X 936754 2.445098511
UA 16X 936809 4.638831704
UA 16X 936864 3.212792628
UA 16X 936914 3.403498281
UA 16X 940084 -3.683286264
UA 16X 940134 3.949742106
DWN 16X 940244 5.031138006
DWN 16X 940309 3.814187618
UA 16X 940364 2.589227288
UA 16X 940409 3.427800184
DWN 16X 940909 3.582933058
DWN 16X 940959 3.493582633
UA 16X 941014 3.664841581
UA 16X 941124 3.321131006
UA 16X 941184 3.784367884
outfile.txt [what i expect]
DWN 16X 936199 3.584936089
DWN 16X 936309 0.930493862
DWN 16X 936374 4.212535002
DWN 16X 936429 3.327646506
UA 16X 936649 3.171373127
UA 16X 936754 2.445098511
UA 16X 936809 4.638831704
UA 16X 936864 3.212792628
UA 16X 936914 3.403498281
UA 16X 940134 3.949742106
DWN 16X 940244 5.031138006
DWN 16X 940309 3.814187618
DWN 16X 940909 3.582933058
DWN 16X 940959 3.493582633
UA 16X 941014 3.664841581
UA 16X 941124 3.321131006
UA 16X 941184 3.784367884
here is my code, which runs, but does not capture all the rows with 'UA' with a positive value.
#!/usr/bin/perl -w
use strict;
use diagnostics;
open(FILER1, "sample.txt") || die "couldn't open the file\n";
open(FILEW1, ">outfile.txt") || die "couldn't create the file\n";
my ($line, $all, $count, $full, $flag, $num);
my (@all);
while(<FILER1>){
$line=<FILER1>;
split /\t/, $line;
if($_[3] >= 0) {
if($line=~/DWN/) {
print FILEW1 "$line";
$flag =1;
}
else {
push(@all, $line);
$count= scalar @all;
$full = join '', @all;
if ($count >=3 && $flag ==1) {
$flag =0;
print FILEW1 "$full";
$count=0;
$full = '';
@all = '';
$all='';
}
}
}
}