在UNIX中以相反顺序获取句子

时间:2013-09-15 16:33:30

标签: linux shell unix

我有一个名为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. 

我该怎么做?

7 个答案:

答案 0 :(得分:7)

您可以使用tac命令:

$ tac file

答案 1 :(得分:1)

你试过这个吗?

tail -r myfile.txt

答案 2 :(得分:1)

只是为了好玩:

nl file | sort -nr | cut -b8-

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

答案 3 :(得分:0)

如果您安装了Ruby:

ruby -e "puts File.readlines('test').reverse" > test_reversed

答案 4 :(得分:0)

您可以使用tail执行此操作:

tail -r file.txt

答案 5 :(得分:0)

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

  

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

答案 6 :(得分:0)

Bash; - )

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

print_reversed < file