简体   繁体   English

Linux的sort命令如何将后续传递应用于数据?

[英]How does Linux's sort command apply subsequent passes to data?

There's an example in A Practical Guide to Linux Commands, Editors, and Shell Programming that looks like this. “Linux实用指南”,“编辑器”和“Shell编程”中有一个示例,如下所示。

You have a file, "fruit", that contains the following: 你有一个文件,“水果”,其中包含以下内容:

Pear
Pear
apple
pear
Apple

Executing this command: 执行此命令:

sort -u -k 1f -k 1 fruit

Results in this output: 结果输出:

Apple
apple
Pear
pear

Why does this work? 为什么这样做? If I execute only the first pass: 如果我只执行第一遍:

sort -u -k 1f fruit

This results in: 这导致:

apple
Pear

The first pass appears to eliminate lines that somehow reappear on the second pass of the former example. 第一遍似乎消除了在前一个例子的第二遍中以某种方式重新出现的线。 Also, since the first pass results in unique lines, I would expect the second pass not to happen at all. 此外,由于第一次传球产生了独特的线条,我预计第二次传球根本不会发生。

By using secondary sorting key, you're telling sort "if the fields are the same, use this to compare them". 通过使用二级排序键,您可以说“如果字段相同,请使用它来比较它们”。 So, -k1f sees Apple and apple as equal, so calls -k1 to compare them. 所以, -k1f认为Appleapple相同,所以调用-k1来比较它们。 The result isn't "equal", so -u doesn't remove anything. 结果不是“相等”,因此-u不会删除任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM