Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to sort text file based on number after first letter

I have a text file in linux that looks like this:

H-988   -0.5418829321861267 no
H-989   -0.5033702254295349 yes
H-990   -1.1516857147216797 hi
H-99    -0.5005123019218445 hello

I want to sort this file based on the number coming after the hyphen. So the order should be:

H-99    -0.5005123019218445 hello
H-988   -0.5418829321861267 no
H-989   -0.5033702254295349 yes
H-990   -1.1516857147216797 hi

I tried the grep sort command but it did not work. For example it puts 95 after 949 instead of before same goes for 99 and 990 as the example provided

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You should sort as numerically,

sort --numeric-sort --field-separator "-" --key 2 some.txt

or a shorter version

sort -n -t "-" -k 2 some.txt
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading