user2813853
user2813853

Reputation:

Read a file backwords line by line in bash

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.

Is there a way to do that in bash?

Upvotes: 0

Views: 264

Answers (1)

fedorqui
fedorqui

Reputation: 290025

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

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

Test

$ 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

Upvotes: 3

Related Questions