-1

I have a requirement to read the file line by line from last line until the first line. Right now I am able to read the file line by line from start with the below piece of code.

while IFS= read line
do
  #Logic here
done <"$Input_File"

Kindly help me out with a solution to read the file line by line from last line.

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
Babu
  • 49
  • 8

2 Answers2

3

You can use tac to read the file from the last line until the first. Using your example you could do:

while IFS= read line
do
  #Logic here
done <<<(tac "$Input_File")

See the manual page for tac (this may not be installed by default in your distribution but should be available using the package manager).

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
0
file="path/to/your/file.txt"
awk '{print NR ":" $0}' $file | sort -t: -k 1nr,1 | sed 's/^[0-9][0-9]*://'
Mowgli
  • 143
  • 2
  • 11