【问题标题】:How to read a file backwards on Linux? [closed]如何在 Linux 上向后读取文件? [关闭]
【发布时间】:2013-02-02 06:38:50
【问题描述】:

我知道我可以使用cat 在 Linux 上从头到尾打印文件中的所有内容。 有没有办法向后做(最后一行)?

【问题讨论】:

  • “阅读”是什么意思?具体说明文件的内容以及您期望“向后”结果的样子。
  • 我想查看 2gb 文件的末尾,所有 2gb 在一行上,所以我做了这个“dd if=file.xml ibs=1 skip=2049186000 count=100” - 意思是从位置 2049186000 开始读取 100 个字节。输出中有一些 dd 内容,但它可以完成工作。

标签: linux cat


【解决方案1】:

是的,你可以使用“tac”命令。

来自 man tac:

Usage: tac [OPTION]... [FILE]...
Write each FILE to standard output, last line first.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -b, --before             attach the separator before instead of after
  -r, --regex              interpret the separator as a regular expression
  -s, --separator=STRING   use STRING as the separator instead of newline
      --help     display this help and exit
      --version  output version information and exit

【讨论】:

    【解决方案2】:

    tac 是一种方式,但并非所有 linux 都默认提供。

    awk 可以这样做:

    awk '{a[NR]=$0}END{for(i=NR;i>=1;i--)print a[i]}' file
    

    【讨论】:

      【解决方案3】:
      sed '1!G;h;$!d' file
      
      sed -n '1!G;h;$p' file
      
      perl -e 'print reverse <>' file
      
      awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' file
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-20
        • 2010-10-25
        • 1970-01-01
        • 2010-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-18
        相关资源
        最近更新 更多