Linux 的 sort 排序指令遇到底線(_) 無法正常排序的解決辦法

在使用 sort的時候,遇到一個問題,我發現包含底線的字串並不能很好的按照預期的順序來排序。比方說這樣:

2
A
_F
G
K
S
x4

sort 之後會變成這樣:

2
A
G
K
S
_F
x4

解決辦法是,使用 sort -V ,代表 natural version sort,

2
A
G
K
S
x4
_F

參考:

command line - Sorting files and folders with underscore at the beginning - Ask Ubuntu

sorting - Unix sort treatment of underscore character - Stack Overflow