在bash中逐行读取文件backword [英] Read a file backwords line by line in bash

查看:71
本文介绍了在bash中逐行读取文件backword的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从头开始逐行读取文件.我尝试了tac,但是它像cat一样工作,我想逐行访问它.

I need to read my file line by line from the end. I tried tac but that works like cat and I want to access it line by line.

有没有办法在bash中做到这一点?

Is there a way to do that in bash?

推荐答案

您可以将tac ...与while循环一起使用:

You can use tac... together with a while loop:

while IFS= read -r line
do
   echo "line is: $line"
done < <(tac file)

测试

$ seq 5 > a
$ while IFS= read -r line; do echo "line is: $line"; done < <(tac a)
line is: 5
line is: 4
line is: 3
line is: 2
line is: 1

这篇关于在bash中逐行读取文件backword的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆