Solved

Linux: Loop through file BACKWARDS

Posted on 2013-11-18
11
446 Views
Last Modified: 2013-11-18
This loops through a file starting at the beginning:
#!/bin/bash
while read LINE
do
      if [ $(( i )) -lt 5 ];
      then 
       i=$((i+1))
       echo $LINE
      else break
      fi
done <"$1"

Open in new window

How can I loop through the file starting at the end and going backward one line at a time?  Can -tail be used for this?  I do not want to read only the last 5 lines because I want to use a more complicated condition than the one above.  Instead, I want to read the file backwards, starting at the last line then moving up one line at a time.
0
Comment
Question by:hankknight
  • 6
  • 3
  • 2
11 Comments
 
LVL 13

Expert Comment

by:duncanb7
ID: 39656652
You want to display the file in reverse order , Right ?

if so , using tac command
tac  youfile.txt>output,txt

some Linux distrubtion, tail -r yourfile.txt that also works
0
 
LVL 16

Author Comment

by:hankknight
ID: 39656732
How can I do this with the code I posted?  This does NOT work:
#!/bin/bash
while read LINE
do
      if [ $(( i )) -lt 5 ];
      then 
       i=$((i+1))
       echo $LINE
      else break
      fi
done < tac "$1"	

Open in new window

0
 
LVL 13

Expert Comment

by:duncanb7
ID: 39656789
what command you input  and what error is ?
Please send  it out
0
 
LVL 13

Accepted Solution

by:
duncanb7 earned 500 total points
ID: 39656835
Is it what you want ?   what is last 5 line meaning

You want display the file in reverse order and at the last-5 line ?

#!/bin/bash
tac $1 >junk.txt
while read LINE
do
      if [ $(( i )) -lt 5 ];
      then
       i=$((i+1))
       echo $LINE
      else break
      fi
done <junk.txt

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 39656889
That does what I want.  Is there a way to do that without creating a temporary junk file?
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 13

Expert Comment

by:duncanb7
ID: 39656909
I think it already asnwered your question
you can  add rm -rf  junk.txt in your script to delete the temp file .
creating a temp file is not harmful
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 39656927
Thanks

Have a nice day


Duncan
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39657202
>> Is there a way to do that without creating a temporary junk file <<

Yes, and please see my last comments to your thread http://www.experts-exchange.com/OS/Linux/Q_28295367.html where I already provided this solution.

#!/bin/bash
while read LINE
do
      if [ $(( i )) -lt 5 ];
      then
       i=$((i+1))
       echo $LINE
      else break
      fi
done <<< $(tac $1)
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 39657235
woolmilkporc,

Yes, it is correct no need for creating temp file from your code.
I tried your code at my side, but the output of result is different from my result.

hankknight,

could you also try it if you have time  ?

Duncan
0
 
LVL 16

Author Comment

by:hankknight
ID: 39657327
<<< $(tac $1)
That does not seem to be working on my OS.  I have posted a question about that here:
http://www.experts-exchange.com/OS/Linux/Q_28297339.html
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39657464
To make this thread complete:

"$(tac $1)" must be enclosed in double quotes in order to keep the newline characters from being stripped off by the shell.

wmp
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now