DEV Community

Cover image for Living in the Shell #6; sort
Babak K. Shandiz
Babak K. Shandiz

Posted on • Originally published at babakks.github.io on

Living in the Shell #6; sort

sort ๐Ÿ†Ž

Sorts/randomizes input lines.

Sort lexically

echo -n 'Zack\nAdam\nCharles' | sort
Enter fullscreen mode Exit fullscreen mode
Adam
Charles
Zack

Sort descending -r

echo -n 'Zack\nAdam\nCharles' | sort -r
Enter fullscreen mode Exit fullscreen mode

Sort numerically -n

echo -n '10 Zack\n5 Adam\n1 Charles' | sort -n
Enter fullscreen mode Exit fullscreen mode
1 Charles
5 Adam
10 Zack

Sort human-readable size numerics -h

echo -n '1G\n1K\n23MB' | sort -h
Enter fullscreen mode Exit fullscreen mode
1K
23MB
1G

Check sorted state -c

echo -n 'Zack\nAdam\nCharles' | sort -c
Enter fullscreen mode Exit fullscreen mode
sort: -:2: disorder: Adam

(Exits with a nonzero exit code that indicates unsorted input.)

Shuffle/randomize -R

echo -n 'Zack\nAdam\nCharles' | sort -R
Enter fullscreen mode Exit fullscreen mode

Latest comments (1)

Collapse
 
waylonwalker profile image
Waylon Walker

What!!?! I never knew sort -h was a thing