I was porting over a script from AIX to linux that had code of the form

grep <pattern> $LOG | sort -b +rn4 -5 +2 -3

On AIX, this kind of sort syntax is documented, and basically the +a -b syntax means skip a fields and consider fields between a and b as your sort key.

This didn't work on linux, because the linux sort command didn't like including the 'rn' (reverse numeric) flags in the +a 'skip fields' parameter. but this did work:

grep <pattern? $LOG | sort -b -rn +4 -5 +2 -3

So apparently the 'field skipping logic is supported by linux sort, but not documented in the man page (that I could see, anyway). the -k option works on both systems to specify a key field number. But here's a weird quirk. On AIX

ls -l | sort -n +4

produces a list of files sorted on the 5th field (size). But on linux, the same command produces an error:

sort: cannot read: +4: No such file or directory

ls -l | sort -n +4 -5

does work, though. So, apparently the + skip - skip key-selection syntax sort of works, but only if you specify both the starting and ending column skip parameters. And it's not documented. So, my question - is this column skipping syntax deprecated? Does it just work because the code was just there in the command and nobody knew to take it out?


I’m assuming your Linux system uses GNU sort; GNU sort’s man page doesn’t provide complete documentation. Instead, the documentation is provided as an Info file (info sort) and is also available in the online Coreutils manual. The latter describes the behaviour you’re seeing:

On systems not conforming to POSIX 1003.1-2001, sort supports a traditional origin-zero syntax ‘+pos1 [-pos2]’ for specifying sort keys. The traditional command ‘sort +a.x -b.y’ is equivalent to ‘sort -k a+1.x+1,b’ if y is ‘0’ or absent, otherwise it is equivalent to ‘sort -k a+1.x+1,b+1.y’.

This traditional behavior can be controlled with the _POSIX2_VERSION environment variable (see Standards conformance); it can also be enabled when POSIXLY_CORRECT is not set by using the traditional syntax with ‘-pos2’ present.

If you run GNU sort with _POSIX2_VERSION set to 199209 in its environment, you’ll be able to use +x on its own:

ls -l | _POSIX2_VERSION=199209 sort -n +4

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.