Hi,
I have a file that looks like this (tab seperated):
Barry -3 -4 -5 -10 -4 6 -8 20 -6 NaN NaN NaN
Brend -2 4 -3 -7 -3 8 -9 -10 -6 NaN NaN NaN NaN NaN
Harry -10 -9 -40 6 -7 3 -7 -2 -5 NaN NaN NaN NaN NaN NaN NaN
I want to print the first column (name) then print the column where theres value equal to or greatr than 0 (along with its position where the counts start from right to left AND also could how many NaN's there are per row.
so the output would look like this
name position|value #of NaN's
Barry 4|6 2|20 3
Brend 7|4 4|8 5
Harry 5|6 7|3 7
hope i was clear
Gisele
second and third line's positions in your sample output are wrong. Here is the code:
$ awk '{printf $1}
{for (i=2;i<=10;i++) if ($i>=0) {printf " "11-i"|"$i} }
{for (j=11;j<=NF;j++) tol++} {print " " tol}
{tol=0} ' urfile
Barry 4|6 2|20 3
Brend 8|4 4|8 5
Harry 6|6 4|3 7
while(<DATA>){
my @tmp=split;
my @t1 = grep { $_ eq "NaN" } @tmp;
print @tmp[0]," ";
@tmp=@tmp[1..$#tmp-$#t1-1];
for (my $i=0;$i<=$#tmp;$i++){
print $#tmp+1-$i,"|",$tmp[$i]," " if $tmp[$i]>0;
}
print " ",$#t1+1,"\n";
}
__DATA__
Barry -3 -4 -5 -10 -4 6 -8 20 -6 NaN NaN NaN
Brend -2 4 -3 -7 -3 8 -9 -10 -6 NaN NaN NaN NaN NaN
Harry -10 -9 -40 6 -7 3 -7 -2 -5 NaN NaN NaN NaN NaN NaN NaN
hey
$ awk '{printf $1}
{for (i=2;i<=10;i++) if ($i>=0) {printf " "11-i"|"$i} }
{for (j=11;j<=NF;j++) tol++} {print " " tol}
{tol=0} ' urfile
works but I have decimal places for some of the #'s so the output comes out a bit different
so the order is all wrong
ex. of input
joe -2 -4 -6 -3 -1 9 3 NaN NaN NaN
output is
joe 4|9 3|3 2|NaN 1|NaN 1
awk '{s=0;p=2;z=0
for (i=2;i<=NF;i++) {
if ($i !~ /[-.0-9]/) {s++; if (z==0) z=i}
else if ($i>0) {$(p++)=i;$(p++)=$i}
}
printf "%s %s|%s %s|%s %s\n",$1,z-$2,$3,z-$4,$5,s}' infile
Barry 4|6 2|20 3
Brend 8|4 4|8 5
Harry 6|6 4|3 7
joe 2|9 1|3 3