排序命令:-g 与 -n 标志

bew*_*ew5 4 command-line

sort命令与-g和一起使用有什么区别-n

我已经尝试了两个标志ls -la并且输出是相同的。

手册页说-g“按一般数值-n比较”和“按字符串数值比较”?

我不明白他们在那里的意思。

“一般数值”是什么意思?“字符串数值”是什么意思?

ste*_*ver 6

主要区别在于对采用科学记数法的数字的处理方式。From info sort,当使用-n(数字)排序时

 Neither a leading `+' nor exponential notation is recognized.  To
 compare such strings numerically, use the `--general-numeric-sort'
 (`-g') option.
Run Code Online (Sandbox Code Playgroud)

所以,例如,给定

$ cat file
+1.23e-1
1.23e-2
1.23e-3
1.23e4
1.23e+5
-1.23e6
Run Code Online (Sandbox Code Playgroud)

然后

$ sort -n file
-1.23e6
+1.23e-1
1.23e-2
1.23e-3
1.23e4
1.23e+5
Run Code Online (Sandbox Code Playgroud)

然而

$ sort -g file
-1.23e6
1.23e-3
1.23e-2
+1.23e-1
1.23e4
1.23e+5
Run Code Online (Sandbox Code Playgroud)


Anw*_*war 4

sort信息页面,排序-g是由这些解释的

\n\n
\xe2\x80\x98-g\xe2\x80\x99\n\xe2\x80\x98--general-numeric-sort\xe2\x80\x99\n\xe2\x80\x98--sort=general-numeric\xe2\x80\x99\n     Sort numerically, converting a prefix of each line to a long\n     double-precision floating point number.  *Note Floating point::.\n     Do not report overflow, underflow, or conversion errors.  Use the\n     following collating sequence:\n\n        \xe2\x80\xa2 Lines that do not start with numbers (all considered to be\n          equal).\n        \xe2\x80\xa2 NaNs (\xe2\x80\x9cNot a Number\xe2\x80\x9d values, in IEEE floating point\n          arithmetic) in a consistent but machine-dependent order.\n        \xe2\x80\xa2 Minus infinity.\n        \xe2\x80\xa2 Finite numbers in ascending numeric order (with -0 and +0\n          equal).\n        \xe2\x80\xa2 Plus infinity.\n\n     Use this option only if there is no alternative; it is much slower\n     than \xe2\x80\x98--numeric-sort\xe2\x80\x99 (\xe2\x80\x98-n\xe2\x80\x99) and it can lose information when\n     converting to floating point.\n
Run Code Online (Sandbox Code Playgroud)\n\n

sort -n是我们通常期望的自然排序

\n\n
\xe2\x80\x98-n\xe2\x80\x99\n\xe2\x80\x98--numeric-sort\xe2\x80\x99\n\xe2\x80\x98--sort=numeric\xe2\x80\x99\n     Sort numerically.  The number begins each line and consists of\n     optional blanks, an optional \xe2\x80\x98-\xe2\x80\x99 sign, and zero or more digits\n     possibly separated by thousands separators, optionally followed by\n     a decimal-point character and zero or more digits.  An empty number\n     is treated as \xe2\x80\x980\xe2\x80\x99.  The \xe2\x80\x98LC_NUMERIC\xe2\x80\x99 locale specifies the\n     decimal-point character and thousands separator.  By default a\n     blank is a space or a tab, but the \xe2\x80\x98LC_CTYPE\xe2\x80\x99 locale can change\n     this.\n\n     Comparison is exact; there is no rounding error.\n\n     Neither a leading \xe2\x80\x98+\xe2\x80\x99 nor exponential notation is recognized.  To\n     compare such strings numerically, use the \xe2\x80\x98--general-numeric-sort\xe2\x80\x99\n     (\xe2\x80\x98-g\xe2\x80\x99) option.\n
Run Code Online (Sandbox Code Playgroud)\n\n

检查Steeldriver 的答案以获得更好的解释。

\n