awk program

I have a csv file as below :

NAME,5a-6a,6a-7a,7a-8a,8a-9a,9a-10a,10a-11a,11a-12n,12n-1p,13p-14p,14p-15p,15p-16p,16p-17p,17p-18p,18p-19p,19p-20p,20p-21p,21p-22p,22p-23p,11p-12m,  TOTALS
,Num/Avg,Num/Avg,Num/Avg,Num/Avg,Num/Avg,Num/Avg,Num/Avg,Num/Avg,Num/Avg,Num/Avg,Num/Avg,Num/Avg,Num/Avg,Num/Avg,Num/Avg,Num/Avg,Num/Avg,Num/Avg,Num/Avg,       Num/Avg
---------------,--------,--------,--------,--------,--------,--------,--------,--------,--------,--------,--------,--------,--------,--------,--------,--------,--------,--------,--------,     -------
Env00,0/0.00,3/0.69,45/2.61,42/2.07,65/2.52,32/2.79,37/2.50,53/1.13,68/3.46,19/2.28,5/0.38,6/7.36,3/3.95,2/0.44,3/9.66,2/5.24,1/0.43,1/11.03,1/0.46,  388/2.58

I need to find out the columns where value before "/" is > 25 && value after "/" > 1

So as per above file, the o/p should be

NAME,7a-8a,8a-9a,9a-10a,10a-11a,11a-12n,12n-1p,13p-14p,   TOTALS
,Num/Avg,Num/Avg,Num/Avg,Num/Avg,Num/Avg,Num/Avg,Num/Avg ,Num/Avg
---------------,--------,--------,--------,--------,--------,--------, --------
Env00,45/2.61,42/2.07,65/2.52,32/2.79,37/2.50,53/1.13,68/3.46, 388/2.58

Try this:

#!/bin/bash
cols=$(awk -F, '
  /^Env00/{for(i=1;i<=NF;i++){
       split($i,a,"/"); 
       if(a[1]>25 && a[2]>1) 
         printf "%d,", i
     }
}' input | sed 's/,$//')  #get the column numbers
cut -d, -f1,$cols input  #extract the first column and the ones stored in $cols
gawk -F, '{
    for(i=1;i<=NF;i++)
        {if(i==1||i==NF){a[NR,i]=$i;c=i}
            else{
                if(NR!=4){a[NR,i]=$i}
                else{split($i,b,"/");if(b[1]>25&&b[2]>1){a[NR,i]=$i;c=i}
                }
            }
        }
    }
    END{
        for(i=1;i<=NR;i++){
            for(j=1;j<=asort(c,d);j++)printf a[i,d[j]] FS
            print ""
        }
    }' urfile

Thanks. but function asort doesn't seem to be working for me

Maybe this:

#!/bin/sh

gawk '
BEGIN {

   FS = OFS = ","

   NumHead = 3   # Assume:  3-header lines
   NumRead = 0   

   # read the three header lines into a 2-D Array ...

   while ( getline > 0 )
   {
      NumRead ++ 

      for ( i = 1 ; i <= NF ; i ++ )
      {
         HeadAry[ NumRead,i ] = Trim( $i )
      }
      if ( NumRead >= NumHead )
      {
         break
      }
   }
   NumMatch = 0
   delete MatchAry       # GLOBAL
}
function DumpData( NumMatch, NumFld,   i, j, D )
{
   if ( NumMatch == 0 )
   {
      delete MatchAry    # GLOBAL
      return( 0 )
   }

   # Dump the three header lines

   for ( i = 1 ; i <= NumHead ; i ++ )
   {
      D = ""
      printf( "%s%s", D, HeadAry[i,1] )
      D = ","

      for ( j = 2 ; j <= NumFld ; j ++ )
      {
         if ( j in MatchAry )
         {
            printf( "%s%s", D, HeadAry[i,j] )
         }
      }
      print ""
   }

   # Dump the Fields from the Current Data Record

   D = ""
   printf( "%s%s", D, $1 )
   D = ","

   for ( j = 2 ; j <= NumFld ; j ++ )
   {
      if ( j in MatchAry )
      {
         printf( "%s%s", D, MatchAry[ j ])
      }
   }
   print ""

   delete MatchAry       # GLOBAL

   return( 0 )
}
function Trim( InStr )
{
   gsub( /^[\t\n\r ]*/, "", InStr )
   gsub( /[\t\n\r ]*$/, "", InStr )

   return( InStr )
}
# main
{
   # check for qualifying fields, insert indices into MatchAry[]

   for ( i = 2 ; i <= NF ; i ++ )
   {
      if (( N = split( $i, SplitAry, "/" )) == 2 )
      {
         if (( SplitAry[1] +0 > 25 ) && ( SplitAry[2] +0 > 1 ))
         {
            NumMatch ++
            MatchAry[ i ] = $i ""
         }
      }
   }
   NumMatch = DumpData( NumMatch, NF )

}' "$@"

Thanks.. I quite didn't understand how do I pass the input file as an argument ??

deo_kaustubh --

Assume your Data is in data.txt ...

  1. Save the CODE as a file, say foo.sh
  2. chmod 755 foo.sh
  3. ./foo.sh data.txt

The "$@" at the end of foo.sh will accept any number of files as input.

-- kjh