Skip to main content
added explanantion
Source Link
RomanPerekhrest
  • 30.9k
  • 5
  • 47
  • 68
awk '/Start|End|Done|dimen/{ print }
     /echo "New Cell"/{ 
         if (f) { 
             if (max >= 35) { 
                 for (i=1; i<=c; i++) print a[i]; 
                 print 
             } 
             delete a; c=max=0 
         } 
         f=1 
     }f{ a[++c]=$0; if ($5 > max) max=$5 }' file

Details:

  • /Start|End|Done|dimen/{ print } - print lines containing Start or End or Done or dimen substring as they are
  • /echo "New Cell"/{ ... } - on encountering line with pattern echo "New Cell":
    • if (f) { ... } - if it's not the 1st pattern line and we have previously processed section:
      • if (max >= 35) - if the maximum among the 5th field values within the recent section is greater of equal to 35:
        • for (i=1; i<=c; i++) print a[i] - print all lines of the recent section (c is the last index of array a and points to the array length)
      • delete a - clear the array a(delete all the items from array a)
      • c=max=0 - reset the crucial variables
    • f=1 - flag activated and indicating active section(a bunch of lines) for processing
  • f{ ... } - on active section being processed:
    • a[++c]=$0 - capture each record/line $0 withing a section into array a. The array is indexed with consecutive numbers by ++c
    • if ($5 > max) max=$5 - determining the maximum max among the 5th field $5 values within a processed bunch of records

awk '/Start|End|Done|dimen/{ print }
     /echo "New Cell"/{ 
         if (f){ 
             if (max >= 35) { 
                 for (i=1; i<=c; i++) print a[i]; 
                 print 
             } 
             delete a; c=max=0 
         } 
         f=1 
     }f{ a[++c]=$0; if ($5 > max) max=$5 }' file
awk '/Start|End|Done|dimen/{ print }
     /echo "New Cell"/{ 
         if (f) { 
             if (max >= 35) { 
                 for (i=1; i<=c; i++) print a[i]; 
                 print 
             } 
             delete a; c=max=0 
         } 
         f=1 
     }f{ a[++c]=$0; if ($5 > max) max=$5 }' file

Details:

  • /Start|End|Done|dimen/{ print } - print lines containing Start or End or Done or dimen substring as they are
  • /echo "New Cell"/{ ... } - on encountering line with pattern echo "New Cell":
    • if (f) { ... } - if it's not the 1st pattern line and we have previously processed section:
      • if (max >= 35) - if the maximum among the 5th field values within the recent section is greater of equal to 35:
        • for (i=1; i<=c; i++) print a[i] - print all lines of the recent section (c is the last index of array a and points to the array length)
      • delete a - clear the array a(delete all the items from array a)
      • c=max=0 - reset the crucial variables
    • f=1 - flag activated and indicating active section(a bunch of lines) for processing
  • f{ ... } - on active section being processed:
    • a[++c]=$0 - capture each record/line $0 withing a section into array a. The array is indexed with consecutive numbers by ++c
    • if ($5 > max) max=$5 - determining the maximum max among the 5th field $5 values within a processed bunch of records

Source Link
RomanPerekhrest
  • 30.9k
  • 5
  • 47
  • 68

Awk solution:

awk '/Start|End|Done|dimen/{ print }
     /echo "New Cell"/{ 
         if (f){ 
             if (max >= 35) { 
                 for (i=1; i<=c; i++) print a[i]; 
                 print 
             } 
             delete a; c=max=0 
         } 
         f=1 
     }f{ a[++c]=$0; if ($5 > max) max=$5 }' file

The output:

echo "Start 2A25.20090401.64809.7.HDF 6420 6751" 
echo "dimensions 9249 49"  
echo "New Cell"   
grep "6621,38" out.2A25.20090401.64809.7.HDF.txt.text = 36.58   7.52  77.82 210
grep "6623,41" out.2A25.20090401.64809.7.HDF.txt.text = 28.62   7.59  77.96 210
grep "6624,40" out.2A25.20090401.64809.7.HDF.txt.text = 33.74   7.52  77.96 210
grep "6624,41" out.2A25.20090401.64809.7.HDF.txt.text = 42.25   7.56  77.99 210
grep "6625,41" out.2A25.20090401.64809.7.HDF.txt.text = 30.58   7.54  78.02 210
grep "6627,40" out.2A25.20090401.64809.7.HDF.txt.text = 34.5   7.46  78.05 200
grep "6628,39" out.2A25.20090401.64809.7.HDF.txt.text = 35.57   7.40  78.05 200
grep "6628,40" out.2A25.20090401.64809.7.HDF.txt.text = 44.82   7.44  78.08 200
grep "6628,41" out.2A25.20090401.64809.7.HDF.txt.text = 21.35   7.47  78.11 210
grep "6629,40" out.2A25.20090401.64809.7.HDF.txt.text = 26.67   7.41  78.11 210
grep "6630,41" out.2A25.20090401.64809.7.HDF.txt.text = 28.12   7.43  78.17 210
grep "6631,40" out.2A25.20090401.64809.7.HDF.txt.text = 21.4   7.37  78.17 272
grep "6631,41" out.2A25.20090401.64809.7.HDF.txt.text = 47.3   7.41  78.20 200
grep "6631,42" out.2A25.20090401.64809.7.HDF.txt.text = 27.42   7.45  78.22 210
grep "6632,41" out.2A25.20090401.64809.7.HDF.txt.text = 39.42   7.39  78.23 200
grep "6632,42" out.2A25.20090401.64809.7.HDF.txt.text = 26.53   7.42  78.25 210
grep "6632,43" out.2A25.20090401.64809.7.HDF.txt.text = 27.72   7.46  78.28 210
grep "6632,44" out.2A25.20090401.64809.7.HDF.txt.text = 41.01   7.50  78.31 210
grep "6631,44" out.2A25.20090401.64809.7.HDF.txt.text = 34.26   7.52  78.28 210
grep "6632,45" out.2A25.20090401.64809.7.HDF.txt.text = 23.02   7.54  78.33 210
grep "6633,41" out.2A25.20090401.64809.7.HDF.txt.text = 32.5   7.36  78.26 272
grep "6633,42" out.2A25.20090401.64809.7.HDF.txt.text = 32.82   7.40  78.28 210
grep "6633,43" out.2A25.20090401.64809.7.HDF.txt.text = 39.28   7.44  78.31 210
grep "6634,41" out.2A25.20090401.64809.7.HDF.txt.text = 26.09   7.34  78.29 272
grep "6634,42" out.2A25.20090401.64809.7.HDF.txt.text = 41.54   7.38  78.31 210
grep "6634,43" out.2A25.20090401.64809.7.HDF.txt.text = 42.26   7.42  78.34 210
grep "6635,42" out.2A25.20090401.64809.7.HDF.txt.text = 31.53   7.36  78.34 210
grep "6635,43" out.2A25.20090401.64809.7.HDF.txt.text = 34.19   7.40  78.37 210
grep "6636,43" out.2A25.20090401.64809.7.HDF.txt.text = 29.9   7.37  78.40 292
echo "New Cell"   
echo "New Cell"   
grep "6646,47" out.2A25.20090401.64809.7.HDF.txt.text = 27.52   7.31  78.81 210
grep "6647,47" out.2A25.20090401.64809.7.HDF.txt.text = 38.67   7.29  78.84 210
grep "6648,47" out.2A25.20090401.64809.7.HDF.txt.text = 37.08   7.27  78.87 210
echo "New Cell"