2

我有一个名为的文本文件test,其中包含如下内容:

Pigeon is the world's oldest domesticated bird. 
Research suggests that domestication of pigeons was as early as ten thousand years ago.
People who keep domestic pigeons are generally called pigeon fanciers.

现在我想要这样的结果,句子顺序相反:

People who keep domestic pigeons are generally called pigeon fanciers.
Research suggests that domestication of pigeons was as early as ten thousand years ago.
Pigeon is the world's oldest domesticated bird. 

我该怎么做呢?

4

8 回答 8

7

您可以使用tac命令:

$ tac file
于 2013-09-15T16:37:48.347 回答
1

你试过这个吗?

tail -r myfile.txt
于 2013-09-15T16:39:20.707 回答
1

纯娱乐:

nl file | sort -nr | cut -b8-

与基于尾部的解决方案不同,它处理整个文件。不过,我不会称其为优雅或高效。

于 2013-09-15T16:41:59.227 回答
0

如果你安装了 Ruby:

ruby -e "puts File.readlines('test').reverse" > test_reversed
于 2013-09-15T16:35:35.877 回答
0

您可以使用tail

tail -r file.txt
于 2013-09-15T16:37:47.850 回答
0

sort -r test应该这样做。从手册页:

-r, --reverse 反转比较结果

于 2013-09-15T16:40:08.670 回答
0

重击 ;-)

function print_reversed {
    readarray -t LINES
    for (( I = ${#LINES[@]}; I; )); do
        echo "${LINES[--I]}"
    done
}

print_reversed < file
于 2013-09-15T16:47:23.780 回答
0

回复上述内容(没有评论的声誉):tail -r不能工作 - 它没有 -r 选项并为此给出“无效选项”错误。

如果输入文件已经排序,sort -r最好是稍微优于 tac(不知何故)。

另外:sed 's/^ *[^ ]* //'比 eg 更快cut -d\ -f2-,所以有趣的建议可能是: nl -w1 file | sort -nr | sed 's/^[^\t]*\t//'

于 2022-02-03T11:24:20.813 回答