Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

help on sorting a file using sort

Tags:

bash

sorting

I have this file:

100: pattern1
++++++++++++++++++++
1:pattern2
9:pattern2
+++++++++++++++++++
79: pattern1
61: pattern1
+++++++++++++++++++

and I want to sort it like this:

++++++++++++++++++++
1:pattern2
9:pattern2
+++++++++++++++++++
61:pattern1
79:pattern1
100:pattern1
+++++++++++++++++++

Is it possible using Linux sort command only ?

If I had :

4:pat1 
3:pat2
2:pat2
1:pat1

O/p should be:

1:pat1
++++++++++++ 
2:pat2
3:pat2
++++++++++++
4:pat1

So, want to sort on first group, but "group" on the pattern of second group. Please note, the thing after : is a regex pattern not a literal.

like image 787
Ankur Agarwal Avatar asked Sep 21 '26 19:09

Ankur Agarwal


1 Answers

Best you can do is to sort it according to the numerical values. But you cannot do anything with the "+"-string.

$ sort -n input
+++++++++++++++++++
+++++++++++++++++++
++++++++++++++++++++
1:wow
9:wow
61: this is it
79: this is it
100: this is it
like image 173
Fredrik Pihl Avatar answered Sep 23 '26 22:09

Fredrik Pihl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!