How to reverse lines of a text file?

后端 未结 9 1059
迷失自我
迷失自我 2021-01-01 10:05

I\'m writing a small shell script that needs to reverse the lines of a text file. Is there a standard filter command to do this sort of thing?

My specific applicatio

相关标签:
9条回答
  • 2021-01-01 10:37
    rev <name of your text file.txt>
    

    You can even do this:

    echo <whatever you want to type>|rev
    
    0 讨论(0)
  • 2021-01-01 10:39

    There is a standard command for your purpose:

    tail -r file.txt
    

    prints the lines of file.txt in reverse order!

    0 讨论(0)
  • 2021-01-01 10:42

    In GNU coreutils, there's tac(1)

    0 讨论(0)
  • 2021-01-01 10:48

    Similar to the sed example above, using perl - maybe more memorable (depending on how your brain is wired):

    perl -e 'print reverse <>'
    
    0 讨论(0)
  • 2021-01-01 10:53

    Answer is not 42 but tac.

    Edit: Slower but more memory consuming using sed

    sed 'x;1!H;$!d;x'
    

    and even longer

    perl -e'print reverse<>'
    
    0 讨论(0)
  • 2021-01-01 10:55
    cat -b only numbers nonblank lines"


    If that's the only issue you want to avoid, then why not use "cat -n" to number all the lines?

    0 讨论(0)
提交回复
热议问题