0

When I run my input file I get a file that contains 758 lines that look like this.

DISTANCIA1.45_SIMETRIA1_GIRO2_ACTIVOS11/MoN-MVW.out:::   Total energy:  
DISTANCIA1.45_SIMETRIA1_GIRO2_ACTIVOS7/MoN-MVW.out:::    Total energy:  
DISTANCIA1.45_SIMETRIA1_GIRO2_ACTIVOS9/MoN-MVW.out:::    Total energy:  
DISTANCIA1.45_SIMETRIA1_GIRO4_ACTIVOS11/MoN-MVW.out:::   Total energy:  

I need to sort so that it looks like this.

DISTANCIA1.45_SIMETRIA1_GIRO2_ACTIVOS7/MoN-MVW.out:::   Total energy:  
DISTANCIA1.45_SIMETRIA1_GIRO2_ACTIVOS9/MoN-MVW.out:::    Total energy:  
DISTANCIA1.45_SIMETRIA1_GIRO2_ACTIVOS11/MoN-MVW.out:::    Total energy:  
DISTANCIA1.45_SIMETRIA1_GIRO4_ACTIVOS11/MoN-MVW.out:::   Total energy: 

In other words I need it to be sorted by the numerical value that comes after the word ACTIVOS.

| improve this question | | | | |
  • 2
    Is the non-numeric prefix always the same length? if so, then something as simple as sort -nk1.38 file might be good enough – steeldriver Dec 27 '18 at 19:55
  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you! – Jeff Schaller Jan 6 '19 at 14:33
0

I was able to accomplish this with the following:

sed 's/ACTIVOS/\&/g' input | sort -n -t \& -k2 | sed 's/\&/ACTIVOS/g'
  1. First we replace ACTIVOS with a single character (because sort can only use a single character delimiter). This is risky if your file is likely to contain the & character but that character can be replaced if you know of another character that certainly will not be in your input.
  2. Now we sort the output numerically based on the second column (everything after the newly added &)
  3. Now we replace the temporary & back with ACTIVOS

With sample input:

$ cat input
DISTANCIA1.45_SIMETRIA1_GIRO2_ACTIVOS11/MoN-MVW.out:::   Total energy:
DISTANCIA1.45_SIMETRIA1_GIRO2_ACTIVOS7/MoN-MVW.out:::    Total energy:
DISTANCIA1.45_SIMETRIA1_GIRO2_ACTIVOS9/MoN-MVW.out:::    Total energy:
DISTANCIA1.45_SIMETRIA1_GIRO4_ACTIVOS11/MoN-MVW.out:::   Total energy:
$ sed 's/ACTIVOS/\&/g' input | sort -n -t \& -k2 | sed 's/\&/ACTIVOS/g'
DISTANCIA1.45_SIMETRIA1_GIRO2_ACTIVOS7/MoN-MVW.out:::    Total energy:
DISTANCIA1.45_SIMETRIA1_GIRO2_ACTIVOS9/MoN-MVW.out:::    Total energy:
DISTANCIA1.45_SIMETRIA1_GIRO2_ACTIVOS11/MoN-MVW.out:::   Total energy:
DISTANCIA1.45_SIMETRIA1_GIRO4_ACTIVOS11/MoN-MVW.out:::   Total energy:

based on your command you should be able to do something like:

grep -R ':: RASSCF root number 1 Total energy:' * | sed 's/ACTIVOS/\&/g' | sort -n -t \& -k2 | sed 's/\&/ACTIVOS/g' > outputCASSCF.txt
| improve this answer | | | | |
  • So I am actually grepping all my energies that pertain to each individual name. like this. grep -R ':: RASSCF root number 1 Total energy:' * > outputCASSCF.txt. inside the output file my output looks like the above. I need it to be sorted numerically by actives, but I need my energies to stay with there pertaining line. – jordan Dec 27 '18 at 19:55
  • @jordan: Does the update to my answer work for you? – jesse_b Dec 27 '18 at 19:58
  • hey thank you so much for helping me. After I run it and open the output file I get this scrip3.sh:grep -R ':: RASSCF root number 1 Total energy:' * | sed 's/ACTIVOS/\ACTIVOS/g' | sort -n -t \ACTIVOS -k2 | sed 's/\ACTIVOS/ACTIVOS/g' > outputCASSCF.txt I get the same thing I wrote on my script but on my output file – jordan Dec 27 '18 at 20:20
  • I'm not sure I understand – jesse_b Dec 27 '18 at 20:22
  • it is not working. after running the file with your code I open my text file and inside is the same code – jordan Dec 28 '18 at 17:15
0

Something like this is good enough when testing with your sample....

cat textfile | sort -V

If you are running sort with GNU extension (or in other words if you are using GNU/Linux or a Unix-like with a sort that have GNU extensions)...

Let me know if it works for all your text, and if so please don't forget to select this as the correct answer.

Good lucky!

| improve this answer | | | | |
0

Another way to approach it would be to delimit the fields somehow; the below uses the underscores to delimit them, then tells sort to sort the numbers starting at position 8 in field 4; in the case of duplicates in field 4, use field 3:

sort -t_ -k4.8n -k -k3,3 input

The --debug output on the sample input is as follows:

DISTANCIA1.45_SIMETRIA1_GIRO2_ACTIVOS7/MoN-MVW.out:::    Total energy:
                                     _
                        _____
________________________________________________________________________
DISTANCIA1.45_SIMETRIA1_GIRO2_ACTIVOS9/MoN-MVW.out:::    Total energy:
                                     _
                        _____
________________________________________________________________________
DISTANCIA1.45_SIMETRIA1_GIRO2_ACTIVOS11/MoN-MVW.out:::   Total energy:
                                     __
                        _____
________________________________________________________________________
DISTANCIA1.45_SIMETRIA1_GIRO4_ACTIVOS11/MoN-MVW.out:::   Total energy:
                                     __
                        _____
________________________________________________________________________
| improve this answer | | | | |

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.