Extracting values after the maximum value in a txt file

Hello,
I'm new to scripting and I need to write a bash script. Here is example of file on which I'm working:

      0.3092381      0.3262799      0.3425480      0.3578379      0.3719490
      0.3846908      0.3958855      0.4053738      0.4130160      0.4186991
      0.4223357      0.4238688      0.4232734      0.4205554      0.4157534
      0.4089370      0.4002056      0.3896858      0.3775293      0.3639085

I try to extract maximum value of this txt file. The maximum value is " 0.4238688 " here.
Then i just want to get values after this maximum value and add zeros for empty spaces. Like this;

     0.4238688      0.4232734      0.4205554      0.4157534      0.4089370
     0.4002056      0.3896858      0.3775293      0.3639085      0.0000000
     0.0000000      0.0000000      0.0000000      0.0000000      0.0000000
     0.0000000      0.0000000      0.0000000      0.0000000      0.0000000

Any suggestions?
Thank you

I don't quite follow what 'add zeros for empty spaces' means, but you can start with below:

tr ' ' '\n' < myFile |sort -nr| paste -d' ' - - - - -

Thank you for your help.

After i get the values following the maximum value, some values are missing and i want to replace this empty spaces with "0.0000000" to keep the number of total value in the txt file.

Sorry, I still don't understand the meaning of some values are missing and i want to replace this empty spaces with "0.0000000"

Sorry for the misunderstanding.

I have a txt file 5x5 like this;

           0.3092381      0.3262799      0.3425480      0.3578379      0.3719490
           0.3846908      0.3958855      0.4053738      0.4130160      0.4186991
           0.4223357      0.4238688      0.4232734      0.4205554      0.4157534
           0.4089370      0.4002056      0.3896858      0.3775293      0.3639085 

I try to extract maximum value of this txt file. The maximum value is " 0.4238688 " here.
Then i just want to get values after this maximum value. Like this;

           0.4238688      0.4232734      0.4205554      0.4157534      0.4089370
           0.4002056      0.3896858      0.3775293      0.3639085 

I have 9 string now. In the beginning i had 25 string because my matrix was 5x5.
Now i try to make this txt file until being 25 string and add some zeros like that;

           0.4238688      0.4232734      0.4205554      0.4157534      0.4089370
           0.4002056      0.3896858      0.3775293      0.3639085      0.0000000
           0.0000000      0.0000000      0.0000000      0.0000000      0.0000000
           0.0000000      0.0000000      0.0000000      0.0000000      0.0000000

Thank you

I guess I don't understand how you get from the original:

0.3092381 0.3262799 0.3425480 0.3578379 0.3719490
0.3846908 0.3958855 0.4053738 0.4130160 0.4186991
0.4223357 0.4238688 0.4232734 0.4205554 0.4157534
0.4089370 0.4002056 0.3896858 0.3775293 0.3639085

matrix to this chopped-off version:

0.4238688      0.4232734      0.4205554      0.4157534      0.4089370
           0.4002056      0.3896858      0.3775293      0.3639085

What determines the chopping-off point?
Why not the entire sorted version of the original 5x5 matrix?

So - "after this maximum" means "by position, by increasing column and then increasing row"?

I did not get the chopped-off version matrix. I wrote just to show in a basic way to make it simpler.

First value will be the maximum value in new txt file "0.4238688" and follows the string. Finally i try to 4*5 matrix again including 20 string like that;

0.4238688      0.4232734      0.4205554      0.4157534      0.4089370 
0.4002056      0.3896858      0.3775293      0.3639085      0.0000000            
0.0000000      0.0000000      0.0000000      0.0000000      0.0000000            
0.0000000      0.0000000      0.0000000      0.0000000      0.0000000

Thank you

Well, I don't get it.
Have you tried what's been posted in post #2 on this thread?

Would this come close (stealing shamelessly from vgersh99)?

awk -vMX=$(tr -s ' ' '\n' < file | sort -nr | head -1) '{for (i=1; i<=NF; i++) {if ($i == MX) RC = 1; if (RC) {print $i; CNT++}}}END {for (i=CNT; i<20; i++) print "0.00"}' file | paste -d" " - - - - -
0.4238688 0.4232734 0.4205554 0.4157534 0.4089370
0.4002056 0.3896858 0.3775293 0.3639085 0.00
0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00

BTW, if you see you're losing people, it might make sense to rephrase, from another point of view.

1 Like

Ah, good guess RudiC - I didn't notice the pattern!

Thank you so much RudiC

It worked. However i have some troubling about sorting (e.g. decimal problem) because i have some values like "3.652067e-08" and because of that sorted numerically wrong.

BTW, I tried to rephrase but i think i failed :slight_smile:

"After that" is a term that is highly dependent on the point of view, esp. in two or more dimensions. Is my observation correct that values increase by increasing column, then row, until reaching the max, then decrease again?

sort can cope with numbers in scientific notation if run with the -g, --general-numeric-sort option (in lieu of the -n ) as it then will "compare according to general numerical value".

Yes you are so right. It worked now :slight_smile: Thank you so much for also English lesson for different point of view i will keep it always in my mind.

Try this (way less effort / resource consumption) if my above observation is correct:

awk -v"MX=1E-100" -vCNT=1 '
        {for (i=1; i<=NF; i++)  if ($i > MX) TMP[1] = MX = $i
                                 else        TMP[++CNT]  = $i
        }
END     {for (i=1; i<=CNT; i++) printf "%9.7g%s", TMP, ++CUR%5?" ":ORS
         for (i=CNT; i<20; i++) printf "%9.7g%s",      0, ++CUR%5?" ":ORS
        }
' file