首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何在Linux上反向读取文件?

如何在Linux上反向读取文件?
EN

Stack Overflow用户
提问于 2013-02-17 21:03:29
回答 3查看 10.6K关注 0票数 5

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-02-17 21:25:40

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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
票数 7
EN

Stack Overflow用户

发布于 2013-02-17 21:03:29

可以,您可以使用tac命令。

来自man tac:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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
票数 14
EN

Stack Overflow用户

发布于 2013-02-17 21:07:28

tac是一种方法,但并非在所有linux上都默认可用。

awk可以这样做:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
awk '{a[NR]=$0}END{for(i=NR;i>=1;i--)print a[i]}' file
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14926024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文