如何对文件进行就地排序

时间:2021-07-10 15:55:49

When we use sort file command, the file shows its contents in a sorted way what if I don't want to get any-kind of output but a sorted file?

当我们使用sort file命令时,文件以排序的方式显示它的内容,如果我不希望得到任何类型的输出,而希望得到排序后的文件呢?

3 个解决方案

#1


210  

You can use file redirection to redirected the sorted output:

您可以使用文件重定向来重定向已排序的输出:

sort input-file > output_file

Or you can use the -o, --output=FILE option of sort to indicate the same input and output file:

或者您可以使用-o, -output=FILE选项来表示相同的输入和输出文件:

sort -o file file

Note: A common mistake is to try to redirect the output to the same input file (e.g. sort file > file). This does not work as the shell is making the redirections (not the sort(1) program) and the input file (as being the output also) will be erased just before giving the sort(1) program the opportunity of reading it.

注意:一个常见的错误是尝试将输出重定向到相同的输入文件(例如,排序文件>文件)。这不起作用,因为shell正在重定向(不是sort(1)程序),而输入文件(作为输出)将在给sort(1)程序读取它的机会之前被擦除。

#2


33  

The sort command prints the result of the sorting operation to standard output by default. In order to achieve an "in-place" sort, you can do this:

排序命令在默认情况下将排序操作的结果打印到标准输出。为了实现“就地”排序,您可以这样做:

sort -o file file

This overwrites the input file with the sorted output. The -o switch, used to specify an output, is defined by POSIX, so should be available on all version of sort:

这将使用已排序的输出覆盖输入文件。用于指定输出的-o开关由POSIX定义,因此应该可以在所有版本的sort中使用:

-o Specify the name of an output file to be used instead of the standard output. This file can be the same as one of the input files.

-o指定要使用的输出文件的名称,而不是标准输出。这个文件可以与一个输入文件相同。

If you are unfortunate enough to have a version of sort without the -o switch (Luis assures me that they exist), you can achieve an "in-place" edit in the standard way:

如果您不幸拥有一个没有-o开关的排序版本(Luis向我保证它们存在),您可以以标准方式实现“就地”编辑:

sort file > tmp && mv tmp file

#3


1  

To sort file in place, try:

要对文件进行排序,请尝试:

echo "$(sort your_file)" > your_file

As explained in other answers, you cannot directly redirect the output back to the input file. But you can evaluate the sort command first and then redirect it back to the original file. In this way you can implement in-place sort.

如其他答案所述,不能直接将输出重定向回输入文件。但是您可以先评估sort命令,然后将它重定向回原始文件。通过这种方式,您可以实现就地排序。

Similarly, you can also apply this trick to other command like paste to implement row-wise appending.

类似地,您也可以将此技巧应用于其他命令,如paste,以实现行级附加。

#1


210  

You can use file redirection to redirected the sorted output:

您可以使用文件重定向来重定向已排序的输出:

sort input-file > output_file

Or you can use the -o, --output=FILE option of sort to indicate the same input and output file:

或者您可以使用-o, -output=FILE选项来表示相同的输入和输出文件:

sort -o file file

Note: A common mistake is to try to redirect the output to the same input file (e.g. sort file > file). This does not work as the shell is making the redirections (not the sort(1) program) and the input file (as being the output also) will be erased just before giving the sort(1) program the opportunity of reading it.

注意:一个常见的错误是尝试将输出重定向到相同的输入文件(例如,排序文件>文件)。这不起作用,因为shell正在重定向(不是sort(1)程序),而输入文件(作为输出)将在给sort(1)程序读取它的机会之前被擦除。

#2


33  

The sort command prints the result of the sorting operation to standard output by default. In order to achieve an "in-place" sort, you can do this:

排序命令在默认情况下将排序操作的结果打印到标准输出。为了实现“就地”排序,您可以这样做:

sort -o file file

This overwrites the input file with the sorted output. The -o switch, used to specify an output, is defined by POSIX, so should be available on all version of sort:

这将使用已排序的输出覆盖输入文件。用于指定输出的-o开关由POSIX定义,因此应该可以在所有版本的sort中使用:

-o Specify the name of an output file to be used instead of the standard output. This file can be the same as one of the input files.

-o指定要使用的输出文件的名称,而不是标准输出。这个文件可以与一个输入文件相同。

If you are unfortunate enough to have a version of sort without the -o switch (Luis assures me that they exist), you can achieve an "in-place" edit in the standard way:

如果您不幸拥有一个没有-o开关的排序版本(Luis向我保证它们存在),您可以以标准方式实现“就地”编辑:

sort file > tmp && mv tmp file

#3


1  

To sort file in place, try:

要对文件进行排序,请尝试:

echo "$(sort your_file)" > your_file

As explained in other answers, you cannot directly redirect the output back to the input file. But you can evaluate the sort command first and then redirect it back to the original file. In this way you can implement in-place sort.

如其他答案所述,不能直接将输出重定向回输入文件。但是您可以先评估sort命令,然后将它重定向回原始文件。通过这种方式,您可以实现就地排序。

Similarly, you can also apply this trick to other command like paste to implement row-wise appending.

类似地,您也可以将此技巧应用于其他命令,如paste,以实现行级附加。