我尝试过以下两个脚本来对 CSV 文件进行排序
$ sort -t"," -k1,1 -k3,3 -k4,4 -k6,6r myFile.csv
和
$ sort -t"," -k1,1 -k3,3 -k4,4 -rk6,6 myFile.csv
我发现有-r之前-k不仅反转第六列,而且反转第一列,同时保持-r后k仅反转第 6 列。一方面,我不明白为什么要放-r之前和之后-k有所作为。另外,我找不到解释帮助或者男人。
答案1
当您使用 时-rk6,6
,该-r
选项将全局应用,即应用于所有键。这就像做:
sort -t"," -r -k1,1 -k3,3 -k4,4 -k6,6 myFile.csv
另一方面,仅-k6,6r
适用r
于第六个字段,即单独应用。
答案2
作为联机帮助页说:
Sort keys can be specified using the options:
-k keydef
The keydef argument is a restricted sort key field definition.
The format of this definition is:
field_start[type][,field_end[type]]
where field_start and field_end define a key field restricted to a
portion of the line (see the EXTENDED DESCRIPTION section), and type is
a modifier from the list of characters 'b' , 'd' , 'f' , 'i' , 'n' ,
'r' .
-k
是一个受限制的定义。您在后面指定的选项-k
仅适用于该字段定义。-r
另一方面,是一个全局选择。再次引用联机帮助页:
The following options shall override the default ordering rules. When
ordering options appear independent of any key field specifications,
the requested field ordering rules shall be applied globally to all
sort keys. When attached to a specific key (see -k), the specified
ordering options shall override all global ordering options for that
key.