Search code examples
notepad++

how to locally sort the lines in NP++


Has any one met such a problem to sort in notepad++?

The text file contains the following lines

0.00000E+00    0.00000E+00    1    4.89402E-02
0.00000E+00    0.00000E+00    3    9.41098E+01
0.00000E+00    0.00000E+00    5    4.22547E+03
0.00000E+00    0.00000E+00    2    7.41087E-06
0.00000E+00    0.00000E+00    4    2.69642E-03
0.00000E+00    0.00000E+00    6    5.35888E-04
0.30000E+02    0.30000E+02    1    2.96322E-02
0.30000E+02    0.30000E+02    3    1.15594E+02
0.30000E+02    0.30000E+02    5    3.30169E+03
0.30000E+02    0.30000E+02    2    7.86962E-03
0.30000E+02    0.30000E+02    4    7.54210E+00
0.30000E+02    0.30000E+02    6    2.03730E+02
0.60000E+02    0.60000E+02    1    4.60685E-03
0.60000E+02    0.60000E+02    3    1.46504E+02
0.60000E+02    0.60000E+02    5    1.31320E+03
0.60000E+02    0.60000E+02    2    4.81953E-04
0.60000E+02    0.60000E+02    4    9.01484E+00
0.60000E+02    0.60000E+02    6    8.16669E+01
0.90000E+02    0.90000E+02    1    7.26858E-05
0.90000E+02    0.90000E+02    3    1.55457E+02
0.90000E+02    0.90000E+02    5    2.43385E+02
0.90000E+02    0.90000E+02    2    3.59327E-03
0.90000E+02    0.90000E+02    4    4.52146E-04
0.90000E+02    0.90000E+02    6    1.65717E-02
0.12000E+03    0.12000E+03    1    7.07527E-03
0.12000E+03    0.12000E+03    3    1.46518E+02
0.12000E+03    0.12000E+03    5    1.30634E+03
0.12000E+03    0.12000E+03    2    6.14906E-03
0.12000E+03    0.12000E+03    4    8.93463E+00
0.12000E+03    0.12000E+03    6    8.16950E+01
...

I need to sort the lines to locally 1~6 on 3rd column

0.00000E+00    0.00000E+00    1    4.89402E-02
0.00000E+00    0.00000E+00    2    7.41087E-06
0.00000E+00    0.00000E+00    3    9.41098E+01
0.00000E+00    0.00000E+00    4    2.69642E-03
0.00000E+00    0.00000E+00    5    4.22547E+03
0.00000E+00    0.00000E+00    6    5.35888E-04
0.30000E+02    0.30000E+02    1    2.96322E-02
0.30000E+02    0.30000E+02    2    7.86962E-03
0.30000E+02    0.30000E+02    3    1.15594E+02
0.30000E+02    0.30000E+02    4    7.54210E+00
0.30000E+02    0.30000E+02    5    3.30169E+03
0.30000E+02    0.30000E+02    6    2.03730E+02
...

I tried to find what: ^(( +\d.\d+E[+|-]\d+){2}) +1(( +-?\d.\d+E[+|-]\d+){1})\r\n^(( +\d.\d+E[+|-]\d+){2}) +3(( +-?\d.\d+E[+|-]\d+){1})\r\n Replace with: \1 1\3\r\n....

But too many tags which I don't think working.


Solution

  • Has any one met such a problem to sort in notepad++

    I have to admit: I have from time to time used Notepad++ for such tasks, when I was too lazy for writing an awk, sed or python script. If the task becomes a tiny bit more complicated then a real scripting language or spreadsheet is the only way.

    But depending on how many lines you have (say a few thousand and not a million) this trick might be worth trying out in Notepad++:

    1. do a Schwartzian transform, that is apply a regex that puts the sorting column at the start of each line:
      • Find What: (([0-9.]+E[-+][0-9]+[ ]+){2})([1-6])(.*)
      • Replace with \3 : \1\3\4
    2. go to first line, first char, record a macro:
      • Macro -> Start recording:
      • mark six lines: (hold down shift and while pressing Cursor Down six times)
      • select "Edit -> Line Operations -> Sort Lines as Integers Ascending
      • press Cursor Down one time (to unselect the six lines and go to the start of the next six line block)
      • Macro -> Stop recording
    3. playback the macro countless times:
      • Press Ctrl-Shift-P and KEEP keys pressed to run over all lines in the file
      • this might sound silly and it will require that you hold the keys pressed for some time, but it will do what you require in a few seconds per thousand lines
    4. undo the Schwartzian transform:
      • replace [1-6] : with empty string

    The trick is:

    1. make the file a bit more suitable for the task by putting the sort field at the front
    2. record a macro
    3. apply the macro as long as necessary using the keyboard shortcut
    4. undo step 1

    And step one is needed so that we can use the sorting feature from the menu in the macro.