0

I have following lines in my file,

echo "Start 2A25.20080401.59125.7.HDF 2831 3230"
echo "dimensions 9248 49"
echo "New Cell"
grep "3065,46" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 28.09 17.2412 78.2198 210 1.83619 6 6
grep "3066,46" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 42.31 17.2616 78.252 210 9.86289
grep "3066,47" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 30.94 17.3031 78.2253 210 2.13253
grep "3067,46" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 31.67 17.2821 78.2842 210 2.93917
echo "New Cell"
grep "3067,17" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 35.32 16.1507 78.9842 210 2.19602 7 7
grep "3067,18" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 35.69 16.1895 78.961 210 6.56008
grep "3067,19" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 33.49 16.2281 78.9379 210 5.46735
grep "3068,16" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 27.31 16.1322 79.0394 210 2.16296
grep "3068,17" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 42.16 16.1711 79.0163 200 4.16615
grep "3068,18" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 48.1 16.2099 78.9931 210 24.642
grep "3068,19" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 49.15 16.2485 78.97 210 29.2187
grep "3069,17" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 33.98 16.1914 79.0484 210 3.68008
grep "3069,18" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 35.04 16.2302 79.0252 210 4.7225
grep "3069,19" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 34.08 16.2688 79.0021 210 6.04774
echo "New Cell"
grep "3069,09" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 31.43 15.8757 79.2349 210 3.33878 8 8
grep "3070,09" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 22.61 15.896 79.2669 292 1.05899
echo "New Cell"
grep "3071,15" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 37.63 16.154 79.159 210 1.20265 9 9
grep "3071,16" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 38.84 16.1932 79.1357 210 7.35424
grep "3072,14" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 24.34 16.1352 79.2142 210 0.616139
grep "3072,15" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 43.13 16.1743 79.1911 210 7.59137
grep "3072,16" ../TextFilesDir/out.2A25.20080401.59125.7.HDF.txt.text = 40.94 16.2135 79.1678 210 14.7196

There are multiple seq of echo "New Cell", Start, dimensions statements.

I have to take the average of 10th column which are 1.823, 9.36 etc. I tried

awk '{ ave+= $10; n=n+1} END { if (n > 0) print ave / n; }'

which sums the 10th column of the entire file and gives me ONE value.

But I want the average of in between the echo "New Cell" lines.

The expected out put should be. 

echo "New Cell" Count =4, average = 3.8 (example, there are 4 lines between 2 echo "new Cell" lines and average of 10th column of these 4 lines is to be taken)

echo "New Cell"

Count = 10, average = 6 (example,there are 10 lines between 2 echo "new Cell" lines and average of 10th column of these 10 lines is to be taken)

and so on. How to add the if statement in awk.

  • I tried to format you text a bit, but the shell commands makes little sense as you have text mixed in with them. Could you please sort it out so that it's readable? – Kusalananda Mar 30 '18 at 17:59
  • (1.83619 + 9.86289 + 2.13253 + 2.93917)/ 4 gives 4.19, but not 3.8 – RomanPerekhrest Mar 30 '18 at 18:07
  • Wait, that thing with shell code and text is your actual input file? – Kusalananda Mar 30 '18 at 18:10
1

Awk solution:

awk '/echo "New Cell"/{    # on encountering line with `echo "New Cell"` 
         if (sum) {        # if calculated sum exists
             c = NR - n;   # get number of processed records
             printf "%s Count = %d, average = %.2f\n", $0, c, sum/c;
             sum = 0
         } 
         n = NR + 1        # get the initial record position/number
     }
     n{ sum += $9 }' file

The output:

echo "New Cell" Count = 4, average = 4.19
echo "New Cell" Count = 10, average = 8.89
echo "New Cell" Count = 2, average = 2.20
  • Assuming the filename in the input data has no spaces. – Kusalananda Mar 30 '18 at 18:19
  • @Kusalananda, it does not make much sense to rely on field numbers if filepath could have space(s). – RomanPerekhrest Mar 30 '18 at 19:33
  • Then maybe delete up to the first = and use that as data? – Kusalananda Mar 30 '18 at 19:35
  • @Kusalananda, then maybe the OP should elaborate if that moment is really required? – RomanPerekhrest Mar 30 '18 at 19:36
  • @geet, see my explanation – RomanPerekhrest Mar 31 '18 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.