# Sort data in order based on values in one columns conditional upon another column [closed]

I have been trying to sort the given input data. And these are the conditions for sorting the data.

• strictly sort the column #1 in increasing order.
• now, within column #1 sort the data in column #2 in increasing order but with condition.

Explanation of the condition:

• column #3 should be grouped by same values (sorting isn't necessary). But, it shouldn't interrupt the sort order in column#2's smallest values.

For eg:

• for group-4 (in col#3) the smallest value in col2 is 15882592 << group-5 (in col#3) smallest value is 15883889; So group-4 should be on top of group-5.

• Similarly, for group-5 (in col3) smallest value is in col2 15883889 << group-1 (in col3) smallest value 15885010; So group-5 should be on top of group-1.

So, finally I have to group col#1 first and then column#2 conditional (grouped) on col#3. Any way to do it using awk, sed, sort or any other unix utilities?

Input Data:

``````2   15881989    6
2   15882091    6
2   15882148    6
2   15882328    6
2   15882364    6
2   15882451    8
2   15882454    8
2   15882493    8
2   15882592    4
2   15882601    4
2   15882607    4
2   15883765    4
2   15883782    4
2   15883783    4
2   15883785    4
2   15883861    4
2   15883862    4
2   15883889    5
2   15883894    5
2   15883904    5
2   15884457    5
2   15884525    5
2   15884546    4
2   15884550    4
2   15884582    4
2   15884613    4
2   15884649    4
2   15884742    4
2   15884965    4
2   15885010    1
2   15885024    1
2   15885061    4
2   15896126    4
3   15896174    4
3   15896152    4
3   15896128    3
3   15896224    3
3   15896258    3
3   15896406    3
``````

Expected output:

``````2   15881989    6
2   15882091    6
2   15882148    6
2   15882328    6
2   15882364    6
2   15882451    8
2   15882454    8
2   15882493    8
2   15882592    4
2   15882601    4
2   15882607    4
2   15883765    4
2   15883782    4
2   15883783    4
2   15883785    4
2   15883861    4
2   15883862    4
2   15884546    4
2   15884550    4
2   15884582    4
2   15884613    4
2   15884649    4
2   15884742    4
2   15884965    4
2   15885061    4
2   15896126    4
2   15896128    4
2   15896152    4
2   15883889    5
2   15883894    5
2   15883904    5
2   15884457    5
2   15884525    5
2   15885010    1
2   15885024    1
3   15896128    3
3   15896224    3
3   15896258    3
3   15896406    3
3   15896152    4
3   15896174    4
``````

Thanks,

## closed as unclear what you're asking by Archemar, cas, Rui F Ribeiro, G-Man, ShadurFeb 19 '18 at 10:28

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• Additional details have been added, as the help center asks. – Gerard H. Pille Feb 23 '18 at 14:41
• I have to add that, I asked the same question on "SO" but requested answer from `pandas` perspective stackoverflow.com/questions/48847508/… . For, the same english language people understood the question and proposed a solution without complaining the english I wrote. But, we have overrated people here who complained first, and then got together to downvote and then close the question. Is this fair? Is this encouraging? I think admin/manager should be more facilitative about people's problem in this forum. – everestial007 Feb 23 '18 at 14:51
• Well, at least one of the downvoters is not overrated and rather broad minded. Myself, I needed the examples to understand your question, but now they're there. Strange. – Gerard H. Pille Feb 23 '18 at 15:42
• Well, overrating doesn't always have to do with real scores they have made on "SE or SO or S..". For, me it is about people assuming that they know things/problems while they don't. Over couple of years I have realized people don't really read question carefully, especially when it's long and multiple parts are involved. I am teacher and I have seen this behavior in the undergrads I teach and also in adults. I too may have done this in certain situations. But, as an adult someone shouldn't call someone "idiosyncratic". And, as an admin the role is even bigger to moderate the problem. – everestial007 Feb 23 '18 at 16:33
• Also, when a question gets downvoted, less and less people get interested it it. Nobody will answer - I have experienced this; and only solution is to delete the question and put a new one. Editing is what "SE, SO" tells us to do, but no 99% of the times it doesn't work; coz people tend to ignore downvoted question and downvotes are not removed even after it's edited. – everestial007 Feb 23 '18 at 16:36

If your data is in /tmp/test.dat, then this gives the output you wanted:

``````sort -k 1n,2n /tmp/test.dat \
| awk '{
if (!(\$1 ":" \$3 in A)) {A[\$1 ":" \$3] = \$2}
print \$1, \$2, \$3, A[\$1 ":" \$3]
}' \
| sort -k 1n -k 4n -k 2n | cut -d " " -f 1-3
``````

I'm adding a fourth column with the minimum of column 2 for each value of column 1 and 3, use that fourth column as second value for a sort, then drop it again.

• This worked for this data, but when I took it to my big data it just didn't work completely. I am thinking something like `sort -k1,1n -R -k3 -k2,2n file.txt` but there `-k22n` is a problem. Is there a way to finally sort `k2` only based on first value of the group in `k3`. This problem is giving me headache. – everestial007 Feb 18 '18 at 0:46
• Please provide some data that resembles reality. It will ease the pain. – Gerard H. Pille Feb 18 '18 at 7:08
• I updated the input and output data. – everestial007 Feb 18 '18 at 14:29
• I only had to use both column 1 and 3 as key to the awk array.. I'll update my answer. – Gerard H. Pille Feb 18 '18 at 15:50
• "sort -R" means random. A joke perhaps? And what is the meaning of "-k22n" ?? – Gerard H. Pille Feb 18 '18 at 16:00