Get the average from column, and eliminate the duplicate values.

Dear Experts,

Kindly help me please,
I have a big file where there is duplicate values in col 11 till col 23, every 2 rows appers a new numbers, but in each row there is different coordinates x and y in col 57 till col 74.
Please i will like to get a single value and average of the x and y coordinates.
Example
Imput file

A         25235.0 21449.012 7 75   1  -3162771 77 43 23 865933.2 1931450.7  22.5   897 102 1                   1T N/A 54000038 0.81383
A         25235.0 21449.012 6 75   1   4163171 79 37 21 865925.2 1931462.8  23.1   897 102 1              P    1T N/A 54000038 0.81383
A         25015.0 20921.01311 75   1  -4153571 75 58 23 857254.8 1920083.9 -22.2   188 103 1              P    1T N/A 54000056 0.81382
A         25015.0 20921.01310 75   2  -4163868 76 36 19 857246.4 1920096.1 -22.2   188 103 1              P    1T N/A 54000056 0.81382
A         25233.0 21449.012 7 75   1   2142770 77 36 25 865970.9 1931408.5  22.9   896 102 1                   1T N/A 54000135 0.81383
A         25233.0 21449.012 6 75   1   3122671 78 44 28 865963.9 1931420.0  23.0   896 102 1                   1T N/A 54000135 0.81383
A         25013.0 20921.01311 75   1  -4132772 76 61 23 857279.7 1920040.5 -22.0   187 103 1                   1T N/A 54000153 0.81382
A         25013.0 20921.01310 75   2  -4122770 77 42 20 857272.1 1920051.7 -22.2   187 103 1              P    1T N/A 54000153 0.81382
A         25011.0 20921.01311 75   1   3195471 76 53 22 857305.0 1919996.0 -21.9   186 103 1              P    1T N/A 54000235 0.81382
A         25011.0 20921.01310 75   2  -4132669 75 38 21 857297.0 1920007.7 -22.1   186 103 1              P    1T N/A 54000235 0.81382
A         25231.0 21449.012 7 75   1  -3122671 78 37 30 865983.2 1931352.7  22.4   964 102 1                   1T N/A 54000253 0.81382
A         25231.0 21449.012 6 75   1  -3132571 80 40 26 865977.8 1931367.7  23.0   964 102 1                   1T N/A 54000253 0.81382

desired output

A         25235.0 21449.012 7 75   1  -3162771 77 43 23 865933.2 1931456.6  22.5   897 102 1                   1T N/A 54000038 0.81383
A         25015.0 20921.01311 75   1  -4153571 75 58 23 857250.6 1920090.0 -22.2   188 103 1              P    1T N/A 54000056 0.81382
A         25233.0 21449.012 7 75   1   2142770 77 36 25 865967.4 1931414.3  22.9   896 102 1                   1T N/A 54000135 0.81383
A         25013.0 20921.01311 75   1  -4132772 76 61 23 857275.9 1920046.1 -22.0   187 103 1                   1T N/A 54000153 0.81382
A         25011.0 20921.01311 75   1   3195471 76 53 22 857301.0 1920001.9 -21.9   186 103 1              P    1T N/A 54000235 0.81382

Thanks in advance :b:

Attached file

With over a hundred posts in these forums, we would hope that you are learning how to do things like this.

What have you tried?

:frowning: Nothing yet?

Your input and output files are in DOS format (with lines terminated by CR and NL) rather than the normal UNIX format (with lines terminated by NL). Furthermore, the last line in both files is incomplete (with no line terminator). You have 11 complete lines and 1 partial line in your input file and 4 complete lines and 1 partial line in your output file.

Do you want to ignore the last two input lines because the last input line is incomplete? Or is there some other reason why there is nothing in your sample output file corresponding to the last two input lines?

Will all of your input files be in DOS format?

Will the last line in all of your input files be missing the line terminator?

Do you want the output file to be in DOS format or UNIX format?

Do you really want the last line of your output file to be missing the line terminator?

You said that there are new values in columns 11-23 every two lines, and that X and Y coordinates for rows with the same value should be averaged. Do we need to verify that columns 11-23 (or is it columns 11-25 or 11-27) match, or can we just get the average coordinates for every pair of lines? (If columns 11-23, 11-25, or 11-27 have the same values on more that two lines, should the coordinates on all consecutive lines with the same values in those columns be averaged, or do you just want to average pairs of lines?)

Dear Don Cragun
Thanks for your support
I use all my files in linux only I have edited the files in windows to send to the forum maybe
This is the problem.
Yes please we need to verify that the columns 11-27 match and get the average x and y can be 2 or more rows . But as I say before in the output will be a single value 11-27.with the average of x and y
Thanks again

[Updated Version]

Try this...

awk '{
    k=substr($0, 11, 13)
    _xy=substr($0, 57, 18);
    split(_xy, aa)
    x[k]+=aa[1]; y[k]+=aa[2]; s[k]++
    if(k in key) next
    key[k]=$0
  }
  END{
    for(k in key){
      _xy=substr(key[k], 57, 18);
      sub(_xy, sprintf("%.1f", x[k]/s[k])" "sprintf("%.1f", y[k]/s[k]), key[k])
      print key[k]
      delete key[k]
    }
  } ' infile

The first line seen is printed after taking the average of the x,y of the subsequent records. The output will not be in the same order as the input.
If you feel, there is an issue with round off - implement the round off function from here https://www.gnu.org/software/gawk/manual/html_node/Round-Function.html

--ahamed

1 Like

You did not answer Don Cragun's question about what you had tried... and your desired output does not match your specification (first line's avarage x is wrong, and the last line is missing). However, try also

awk     '                       {A = substr ($0,11,17); B = substr ($0, 57, 8); C = substr ($0, 66, 9)}
         A != OA && NR > 1      {printf "%s%6.1f %6.1f%s\n", substr(D,1,56), SUM1/CNT, SUM2/CNT, substr(D,75); CNT=0; SUM1=SUM2=""}
         END                    {printf "%s%6.1f %6.1f%s\n", substr(D,1,56), SUM1/CNT, SUM2/CNT, substr(D,75); CNT=0; SUM1=SUM2=""}
                                {OA = A; D=$0; SUM1+=B; SUM2+=C; CNT++}
        ' file
A         25235.0 21449.012 6 75   1   4163171 79 37 21 865929.2 1931456.8  23.1   897 102 1              P    1T N/A 54000038 0.81383
A         25015.0 20921.01310 75   2  -4163868 76 36 19 857250.6 1920090.0 -22.2   188 103 1              P    1T N/A 54000056 0.81382
A         25233.0 21449.012 6 75   1   3122671 78 44 28 865967.4 1931414.2  23.0   896 102 1                   1T N/A 54000135 0.81383
A         25013.0 20921.01310 75   2  -4122770 77 42 20 857275.9 1920046.1 -22.2   187 103 1              P    1T N/A 54000153 0.81382
A         25011.0 20921.01310 75   2  -4132669 75 38 21 857301.0 1920001.9 -22.1   186 103 1              P    1T N/A 54000235 0.81382
A         25231.0 21449.012 6 75   1  -3132571 80 40 26 865980.5 1931360.2  23.0   964 102 1                   1T N/A 54000253 0.81382
1 Like

I was working on something very similar to what RudiC posted, but he finished it before I did.

For the given sample data it won't make any difference, but to preserve the column positions if some of the X and Y coordinates have smaller values, you might want to change the format strings slightly from:

         A != OA && NR > 1      {printf "%s%6.1f %6.1f%s\n", substr(D,1,56), SUM1/CNT, SUM2/CNT, substr(D,75); CNT=0; SUM1=SUM2=""}
         END                    {printf "%s%6.1f %6.1f%s\n", substr(D,1,56), SUM1/CNT, SUM2/CNT, substr(D,75); CNT=0; SUM1=SUM2=""}

to:

         A != OA && NR > 1      {printf "%s%8.1f %9.1f%s\n", substr(D,1,56), SUM1/CNT, SUM2/CNT, substr(D,75); CNT=0; SUM1=SUM2=""}
         END                    {printf "%s%8.1f %9.1f%s\n", substr(D,1,56), SUM1/CNT, SUM2/CNT, substr(D,75); CNT=0; SUM1=SUM2=""}

And, in ahmed101's code the change would be from:

      sub(_xy, sprintf("%.1f", x[k]/s[k])" "sprintf("%.1f", y[k]/s[k]), key[k])

to:

      sub(_xy, sprintf("%8.1f", x[k]/s[k])" "sprintf("%9.1f", y[k]/s[k]), key[k])
3 Likes

Dear All ,
Thanks a lot for your help.. I appreciate your time and support.
it works fine.. thanks again

---------- Post updated at 01:13 PM ---------- Previous update was at 01:11 PM ----------

Don Cragun,, Thanks a lot :b:

---------- Post updated at 01:14 PM ---------- Previous update was at 01:13 PM ----------

ahamed101 and RudiC,, thanks for your help