0
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"   
grep "6622,14" out.2A25.20090401.64809.7.HDF.txt.text = 22.24   6.61  77.22 291
grep "6622,15" out.2A25.20090401.64809.7.HDF.txt.text = 29.63   6.65  77.25 291
grep "6623,15" out.2A25.20090401.64809.7.HDF.txt.text = 30.63   6.63  77.28 291
echo "New Cell"   
grep "6630,45" out.2A25.20090401.64809.7.HDF.txt.text = 31.44   7.59  78.27 292
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"   

My Text file is shown above. The Column5 is the value that I am interested. In the last line posted above, its value is 37.08.

I want to retain only the groups (which are separated by echo "New Cell") in which at least one value is >=35

I tried:

awk '$5>=35 || /New Cell/ || /Start/ || /dimen/ || /Done/ || /End/' \
  CWoSpix.apr2009.sh*

But it is removing all the lines in which 5th column value is <35.

The Expected Output is:

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 "6630,45" out.2A25.20090401.64809.7.HDF.txt.text = 31.44   7.59  78.27 292
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"   

How can awk can be used to get the desired output?

3
  • why does this line grep "6630,45" out.2A25.20090401.64809.7.HDF.txt.text = 31.44 ... appear in the output as it does not fit the condition $5>=35? Elaborate your output Commented Jan 3, 2018 at 11:30
  • you should edit your question to clarify what it is that you actually want. include the fact that you want to output not just individual lines, but the groups of data containing at least one line with $5 >= 35.
    – cas
    Commented Jan 3, 2018 at 11:47
  • also, try describing what it is you are trying to do because if that's supposed to be a shell script, it won't run. the args to grep are wrong. It looks like you're trying to summarise the results of many grep commands on one file. if so, you'd be better off writing an awk script to do that job and summarise the results. This is looking like an XY Problem.
    – cas
    Commented Jan 3, 2018 at 11:51

1 Answer 1

1

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

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

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"
3
  • @geet, ok, see my explanation Commented Jan 3, 2018 at 13:52
  • Hi Roman, the above awk script works. but My input file has multiple Start and End statements.echo "Start 2A25.20080302.58657.7.HDF 5959 6333" echo "dimensions 9246 49" echo "New Cell" echo "All Done" echo "End 2A25.20080302.58651.7.HDF 2469 2611" echo "Start 2A25.20080302.58657.7.HDF 5959 6333" echo "dimensions 9246 49" echo "New Cell" echo "All Done" echo "Start 2A25.20080303.58673.7.HDF 6467 6749" echo "dimensions 9246 49" echo "New Cell" echo "All Done" echo "End 2A25.20080302.58657.7.HDF 5959 6333" which should not b repeated.
    – geet
    Commented Feb 13, 2018 at 11:30
  • @geet, instead of diving into ancient question - create new question with new conditions Commented Feb 13, 2018 at 11:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .