Determining position in a tab delimited file

hi, I want to determine the position of specific values over a cutoff.

So I have a string of values that are mainly negative in number and I want to print the rare few that are positive. Specifically I want to know the position of the value along the string. The position is based from right to left (the right most value is 1 etc..). At the same time it should ignore the UI's at the end.

e.g. (note the file is tab delimited)

name  x  -0.002   -92  -45  78.33  UI  UI  UI
name  y  -9.55    -83.21  45  -9.34  UI  UI  UI

I want the output to look like this

name  x  1
name  y  2

kinda tricky:confused:

where's your code? you know about awk or the shell already from previous posts. So try to do it yourself first. Read the docs, always!!

Hi,
Try this,

#!/usr/bin/perl

while (<>) {
        chomp $_;
        @array = split;
        print $array[0],"\t",$array[1],"\t";
        $counter = 0;
        for (reverse (@array)){
        chomp;
        if ( ! /UI/ ){
                $counter ++ ;
                if ($_ > 0 ) {
        print $counter,"\t",$_,"\n";
        }
        }
        }
}

perl test27.pl sample.txt

Output

name    x       1       78.33
name    y       2       45

Is this homework or are you working on the same project than this poster?

A solution...

cat file | sed 's/[ UI]*$//g'  |  awk '{for(i=1;i<=NF;i++)a=$i}{for(i=1;i<=NF;i++)if(a>0 && a~/[0-9]/)print $1,$2,(NF+1)-i}'

I'm a beginner too, then i try to solve the problem....it's perhaps not the better solution.

On the sample given above, this could also work:

 awk -F"\t" '{for(i=3;i<=6;i++) if($i>=0) $3=(7-i)}{print $1,$2,$3}' file

hey this code works great but whenever it runs into a line with multiple positives, it gives an output that looks like this..

name    x       1       78.33
1     56
name    y       2       45

how would I modify the below code so I get the name and x representing the outputs

name    x       1       78.33
name    x       1       56
#!/usr/bin/perl

while (<>) {
        chomp $_;
        @array = split;
        print $array[0],"\t",$array[1],"\t";
        $counter = 0;
        for (reverse (@array)){
        chomp;
        if ( ! /UI/ ){
                $counter ++ ;
                if ($_ > 0 ) {
        print $counter,"\t","$_","\n";
        }
        }
        }
}

I tried modifying it but no success.

Modiefied, try this ..

#!/usr/bin/perl
while (<>) {
        chomp $_;
        @array = split;
        print $array[0],"\t",$array[1],"\t";
        $counter = 0;
        for (reverse (@array)){
        chomp;
        if ( ! /UI/ ){
                $counter ++ ;
                if ($_ > 0 ) {
        print $counter,"\t","$_","\n";
        last;
        }
        }
        }
}

input file

name  x  -0.002   -92  -45  78.33  UI  UI  UI
name  y  -9.55    -83.21  33    45  -9.34  UI  UI  UI

output

name    x       1       78.33
name    y       2       45

Hey actually the original script worked better. So the output using the original script would give.

name    x    1    78.33
name    y    2    45
3    33

That is perfectly correct but I want it to have

name x 1 78.33
name y 2 45
name y 3 33

i have no clue how to add the name and y (or x) for the other values.

awk '{gsub(" UI","");for(i=2;++i<=NF;){if($i>=0)print $1,$2,(NF-i+1),$i}}' OFS='\t' file

From here I'll let you sort out the output :wink:

thank you danmero!!!!!!!

Hi,

Try this...........

#!/usr/bin/perl

while (<>) {
        chomp $_;
        @array = split;
        $counter = 0;
        for (reverse (@array)){
        chomp;
        if ( ! /UI/ ){
                $counter ++ ;
                if ($_ > 0 ) {
        print $array[0],"\t",$array[1],"\t",$counter,"\t",$_,"\n";
        }
        }
        }
}