2

I try to sort the following file:

$ cat testfile
ZBOX-BATCH-SAMEDI;CMC10
ZBOX-BATCH-SAMEDI_D1;HEB05
ZBOX-BATCH-SAMEDI;BMD15
ZBOX-BATCH-SAMEDI_D3;HEB03
ZBOX-BATCH-SAMEDI_D3_M1;ODP47
ZBOX-BATCH-SAMEDI_D1_M1;TER23
ZBOX-BATCH-SAMEDI_D3;HEB02
ZBOX-BATCH-SAMEDI_D1;HEB02
ZBOX-BATCH-SAMEDI;DEA11
ZBOX-BATCH-SAMEDI;DEA11A

And I get:

$ sort testfile
ZBOX-BATCH-SAMEDI;BMD15
ZBOX-BATCH-SAMEDI;CMC10
ZBOX-BATCH-SAMEDI_D1;HEB02
ZBOX-BATCH-SAMEDI_D1;HEB05
ZBOX-BATCH-SAMEDI_D1_M1;TER23
ZBOX-BATCH-SAMEDI_D3;HEB02
ZBOX-BATCH-SAMEDI_D3;HEB03
ZBOX-BATCH-SAMEDI_D3_M1;ODP47
ZBOX-BATCH-SAMEDI;DEA11
ZBOX-BATCH-SAMEDI;DEA11A

However, I was expecting:

ZBOX-BATCH-SAMEDI;BMD15
ZBOX-BATCH-SAMEDI;CMC10
ZBOX-BATCH-SAMEDI;DEA11
ZBOX-BATCH-SAMEDI;DEA11A
...

That is, I want all the rows with the string ZBOX-BATCH-SAMEDI before the first semicolon to come first, then the ones with ZBOX-BATCH-SAMEDI_D1, etc.

I looked man sort and I tried different options like -g and -k with -t, but I didn't manage. How can I do it?

4
  • 2
    @Robert Harvey such questions are asked quite often here, I don't understand why it was closed. See stackoverflow.com/questions/tagged/sorting+unix Commented May 28, 2015 at 10:13
  • Your question isn't answerable anyway. You told us what you were expecting, but not what you actually got. Commented May 28, 2015 at 10:14
  • @RobertHarvey yes the OP posted it in a very bad way. However, after some cleaning it is quite clear what he wants. What I don't know if it is suited to be reopened or should still be off-topic. Commented May 28, 2015 at 10:19
  • unix.stackexchange.com/q/35469 Commented May 28, 2015 at 10:24

1 Answer 1

4

Just tell sort that the delimiter is ; and you want to sort based on the second column:

sort -t";" -k2 file

It returns:

ZBOX-BATCH-SAMEDI;BMD15
ZBOX-BATCH-SAMEDI;CMC10
ZBOX-BATCH-SAMEDI;DEA11
ZBOX-BATCH-SAMEDI;DEA11A
ZBOX-BATCH-SAMEDI_D1;HEB02
ZBOX-BATCH-SAMEDI_D3;HEB02
ZBOX-BATCH-SAMEDI_D3;HEB03
ZBOX-BATCH-SAMEDI_D1;HEB05
ZBOX-BATCH-SAMEDI_D3_M1;ODP47
ZBOX-BATCH-SAMEDI_D1_M1;TER23
Sign up to request clarification or add additional context in comments.

2 Comments

Somehow this doesn't work as I expect it to work. If I do echo -e "1\n10" | sort -t";" -k1 I get 1 and 10 like I expect lexicographical sorting to work. But with echo -e "1;bbb\n10;aaa" | sort -t";" -k1 I get the line with 10 first and then the 1. What am I missing here? Strangely enough it works for echo -e "x\nxx" | sort -t";" -k1 and echo -e "x;bbb\nxx;aaa" | sort -t";" -k1 -- here in both cases "x" < "xx". Funnily enough if I do add -g (general numeric sort) its fine (1<10, and x<xx work too) it works. And echo -e "1;bbb\n11;aaa" | sort -t";" -k1,1 works too. Hrm...
@towi strange, indeed. I suggest you to post a new question over this: we most probably are missing some parts of the docs, or some undocumented bit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.