2013-02-17 72 views
3

我知道我可以使用cat在Linux上從頭到尾打印文件中的所有內容。 有沒有辦法做到這一點(最後一行)?如何在Linux上向後讀取文件?

+0

你是什麼意思的 「讀」 嗎?具體說明文件的內容以及您期望的「倒退」結果。 – 2013-02-18 10:44:41

+0

我想看看2GB文件的結尾,所有2GB在一行上,所以我做了這個「dd if = file.xml ibs = 1 skip = 2049186000 count = 100」 - 意思是從位置2049186000開始讀取100個字節。輸出有一些dd的東西,但它的工作。 – bobef 2014-05-23 02:47:15

回答

6
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 
9

是的,你可以使用「tac」命令。

從人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 
4

tac是一種方式,但不是默認適用於所有的Linux操作系統。

awk中能做到這一點,如:

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