Resume and count repeated values

Gents,

Please can you help me.

Input file

1050    , 9    ,9888    
1050    ,10    ,9888    
1050    ,11    ,9888    
1050    ,13    ,9888    
1050    ,15    ,9888    
1051    , 9    ,9889    
1051    ,12    ,9889    
1051    ,15    ,9889    
1051    ,18   ,9889    
1052    , 9    ,9890    
1052    ,10    ,9890    
1052    ,11    ,9890    
1052    ,15    ,9890    
1053    , 9    ,9891    
1053    ,10    ,9891    
1053    ,14    ,9891    
1054    , 9    ,9892    
1054    ,10    ,9892    
1054    ,11    ,9892    

Kindly help me to get the minimun and maximun value in column 2, for each value in column 1. Then to write in a new column 4 the total of values counted.

My desired output shoul be like this:

1050    9  15    9888   5     
1051    9  18    9889   4     
1052    9  15    9890   4     
1053    9  14    9891   4     
1054    9  11    9891   3   

Thanks in advance :b:

awk -F, -f ji.awk OFS='\t' myFile
where ji.awk:

{
  if (!($1 in mi) || $2<mi[$1]) mi[$1]=$2
  if (ma[$1]<$2) ma[$1]=$2
  c[$1]++
  l[$1]=$NF
}
END {
  for (i in l)
    print i,mi,ma,l,c
}
1 Like

try also (sorted output):

awk '
{for (i=1; i<=NF; i++) $i=$i+0;
 if (!a[$1]) mx[$1]=mn[$1]=$2;
 a[$1]=$1; b[$1]=$NF; c[$1]++;
 if ($2<mn[$1]) mn[$1]=$2;
 if ($2>mx[$1]) mx[$1]=$2;
}
END {
  for (i in a) print i, mn, mx, b, c;
}
' FS="," OFS="\t" infile
1 Like

Your help is really appreciated .. Thanks a lot

---------- Post updated at 04:05 AM ---------- Previous update was at 03:50 AM ----------

Hi rdtrx1,

How I can print other column using your script ( string value ).. I try to modify myself ,, but I got value 0,, in the colum :(.. I try to use printf instead of print ..but it doesn't work...

 for (i in a) print i, mn, mx, b, c;

Input file

1050    , 9    ,9888    ,N8        
1050    ,10    ,9888    ,N8        
1050    ,11    ,9888    ,N8        
1050    ,12    ,9888    ,N8        
1050    ,13    ,9888    ,N8        
1051    , 9    ,9889    ,N8        
1051    ,10    ,9889    ,N8        
1051    ,11    ,9889    ,N8        
1051    ,12    ,9889    ,N8        
1052    , 9    ,9890    ,N8        
1052    ,10    ,9890    ,N8        
1052    ,11    ,9890    ,N8        
1052    ,12    ,9890    ,N8        
1053    , 9    ,9891    ,N8        
1053    ,10    ,9891    ,N8        
1053    ,11    ,9891    ,N8        
1054    , 9    ,9892    ,N8        
1054    ,10    ,9892    ,N8        
1054    ,11    ,9892    ,N8    

output file desired..

1050    9  15    9888   N8  5 
1051    9  18    9889   N8  4 
1052    9  15    9890   N8  4 
1053    9  14    9891   N8  4 
1054    9  11    9892   N8  3 

Thanks for your help

---------- Post updated at 04:55 AM ---------- Previous update was at 04:05 AM ----------

I try like it... but it doesn't work :frowning:

awk '                                                                                     
{for (i=1; i<=NF; i++) $i=$i+0;                                                           
 if (!a[$1]) mx[$1]=mn[$1]=$2;                                                            
 a[$1]=$1; b[$1]=$3; d[$1]=$4; c[$1]++;                                                   
 if ($2<mn[$1]) mn[$1]=$2;                                                                
 if ($2>mx[$1]) mx[$1]=$2;                                                                
}                                                                                         
END {                                                                                     
  for (i in a) print i, mn, mx, b, d, c;                                   
# for (i in a) printf i ("%10d,%10d,%10d,%10d,%10s\n", mn, mx, b, d, c;    
}                                                                                         
' FS="," OFS="\t" tmp10                                                                   

Try

$ sed 's/,//g' file | awk '{A[$1]++;B[$1]=$3;
less[$1]=$2<less[$1]||less[$1]?less[$1]:$2;
more[$1]=more[$1]>$2?more[$1]:$2;}
END{for(i in A)
{print i,less,more,B,A}}'

1050 9 15 9888 5
1051 9 18 9889 4
1052 9 15 9890 4
1053 9 14 9891 3
1054 9 11 9892 3
1 Like

Dear Pamu,

How I can get the column with N8 value as follwing:

1050    9  15    9888   N8  5 

Thanks

$ awk '{gsub(",","");A[$1]++;B[$1]=$3;C[$1]=$4;
less[$1]=$2<less[$1]||less[$1]?less[$1]:$2;
more[$1]=more[$1]>$2?more[$1]:$2;}
END{for(i in A)
{print i,less,more,B,C,A}}' file

1050 9 13 9888 N8 5
1051 9 12 9889 N8 4
1052 9 12 9890 N8 4
1053 9 11 9891 N8 3
1054 9 11 9892 N8 3
1 Like

Dear Pamu

Thanks a lot it works perfect... :slight_smile:

---------- Post updated at 12:34 PM ---------- Previous update was at 09:05 AM ----------

Thanks Pamu.. it works perfect :slight_smile: