bash sort from the end of string

1

I want to sort file by first file, but the last character is the most important.

For example form file:

"AAAACTTTTT"                    1       2

"AAAACAAAAA"    1       2       6       4

To:

"AAAACAAAAA"    1       2       6       4

"AAAACTTTTT"                    1       2

I tried:

sort -k1,1 file

Of course it doesn't work, but I have no idea how I can do this. Could you help me? Mayby some secret flags?

diego9403

Posted 2016-06-02T14:16:08.127

Reputation: 807

You can try rev which reverse the line (delimiter is "\n"). So you can do it like this cat file|rev|sort -k1,1 |rev – makgun – 2016-06-02T16:23:31.023

But now I don't know how many is filed and I can't pick field. – diego9403 – 2016-06-02T18:45:49.180

Sorry please more information (I have learnt English but not well) So I don't understand what you couldn't do – makgun – 2016-06-02T18:59:17.120

My English is very poor that can be a problem. I want to sort file contain, that kind of lines as I wrote and field to sort is 1 - "AAACTTT" and I want to sort this as "TTTCAAA". – diego9403 – 2016-06-02T19:03:50.007

Do you want to implement this for each field? – makgun – 2016-06-02T19:07:25.437

No only for this one. – diego9403 – 2016-06-02T19:15:55.943

Try this one cat YourFile|sed 's/\([^ ]*\)\(.*\)/echo -e $(echo \1|rev)\2/g'|bash|sort |sed 's/\([^ ]*\)\(.*\)/echo -e $(echo \1|rev)\2/g'|bash – makgun – 2016-06-02T19:41:12.553

also you can try first part of it to understand how is it working. Firstly try cat YourFile|sed 's/\([^ ]*\)\(.*\)/echo -e $(echo \1|rev)\2/g'|bash secondly try this cat YourFile|sed 's/\([^ ]*\)\(.*\)/echo -e $(echo \1|rev)\2/g'|bash|sort this will give you how it is working – makgun – 2016-06-02T19:42:57.983

I hope this will help you. The command look like very long but it is as fast as what you expected. There is also awk way but I am not enough good in awk. – makgun – 2016-06-02T19:49:03.233

Answers

1

I wanted to sort (group) a list of email addresses by their domain names e.g. all '@xxx.com' then all the '@yyy.com' (solution based on a comment by makgun)

rev emailist | sort | rev > emailistsorted

to just get uniques

rev emailist | sort -u | rev > emailistsorted

zzapper

Posted 2016-06-02T14:16:08.127

Reputation: 231

0

If the file isn't too big, you can do it with GNU awk:

{
    count = split($1, arr, "");
    key = ""
    for (i = 1; i <= count; i++) {
        key = arr[i] key;
    }
    lines[key] = $0;
}
END {
    count = asort(lines);
    for (i = 1; i <= count; i++) {
        print lines[i];
    }
}

Michael Vehrs

Posted 2016-06-02T14:16:08.127

Reputation: 255