Solved

Linux: Loop through file BACKWARDS

Posted on 2013-11-18
11
458 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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
 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Fine Tune your automatic Updates for Ubuntu / Debian
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

809 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