Solved

Linux: Loop through file BACKWARDS

Posted on 2013-11-18
11
481 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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.

728 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