Solved

Linux: Loop through file BACKWARDS

Posted on 2013-11-18
11
452 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
Fine Tune your automatic Updates for Ubuntu / Debian
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

773 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