3

How to get sort command working with data separated by "|" (pipe character) ?

Consider this sample file called "x" which have two fields:

axis-AXIS|OK
axis-AXISR|OK
axis-AXISD2X|OK
axis-AXISA|OK
axis-AXISD1|OK
axis-AXIS1RX|OK
axis-AXISR1M|OK
axis-AXISD2|FALSE
axis-AXISMD|FALSE
axis-AXISM|FALSE
axis-AXISE|OK
axis-AXISD|OK

My locale:

echo $LC_ALL
C

When it is sorted the character "|" seems to be part of the key producing a strange assortment:

sort -t"|" x

axis-AXIS1RX|OK
axis-AXISA|OK
axis-AXISD1|OK
axis-AXISD2X|OK
axis-AXISD2|FALSE
axis-AXISD|OK
axis-AXISE|OK
axis-AXISMD|FALSE
axis-AXISM|FALSE
axis-AXISR1M|OK
axis-AXISR|OK
axis-AXIS|OK

Specifying a field key sort -t"|" -k1 x nothing changes.

  • Am I going blind?  You say, "Specifying a field key, the assortment changes ...", but I don't see any difference in the output.  … … … … … … … … … … … … … … … … … … … … … … … … … … …  P.S. I believe that you mean echo "$LC_ALL", not echo `$LC_ALL`. – G-Man Sep 2 '15 at 22:07
  • Copy&Paste on typing mistake I'll fix it. – Luciano Sep 2 '15 at 22:16
5

Without the closing field number with -k, it will consider all fields starting from 1. You need :

sort -k1,1 -t'|' file.txt

Test :

$ sort -k1,1 -t'|' file.txt 
axis-AXIS|OK
axis-AXIS1RX|OK
axis-AXISA|OK
axis-AXISD|OK
axis-AXISD1|OK
axis-AXISD2|FALSE
axis-AXISD2X|OK
axis-AXISE|OK
axis-AXISM|FALSE
axis-AXISMD|FALSE
axis-AXISR|OK
axis-AXISR1M|OK

If your locale is not C, you can set it on the fly :

LC_ALL=C sort -k1,1 -t'|' file.txt

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.