Solved

Linux: Loop through file BACKWARDS

Posted on 2013-11-18
11
450 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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.:
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.

867 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

21 Experts available now in Live!

Get 1:1 Help Now