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


"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?

  • 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 Jun 2 '16 at 16:23
  • But now I don't know how many is filed and I can't pick field. – diego9403 Jun 2 '16 at 18:45
  • Sorry please more information (I have learnt English but not well) So I don't understand what you couldn't do – makgun Jun 2 '16 at 18:59
  • 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 Jun 2 '16 at 19:03
  • Do you want to implement this for each field? – makgun Jun 2 '16 at 19:07

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

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;
    count = asort(lines);
    for (i = 1; i <= count; i++) {
        print lines[i];

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.