Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, Apple iPhone, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

How do I read prior lines in a text file?

I want to compare a line in a file with zero(0) and if its more than zero i want to print the prior two lines, can u please help me in solving out this... please


Dave's Answer:

I read this a couple of times and think I know what you're asking, but it depends on whether I'm correct in understanding the format of the data file you're talking about and the specific task you face.

Let's consider a data file like this:

a critical line of data
and a second one, followed by
7
and another line of data

Let's look at the task "check every line to see if it's a number, then test to see if it's greater than zero. If it is, show the prior two lines".

Honestly, the trickiest part is to test a line of text to see if it's a number or not. Here's how I solve that:

lineWithoutDigits="$(echo $input | sed 's/[^0-9]//g')"

  if [ "$lineWithoutDigits" = "$input" ] ; then
    if [ $input -gt 0 ] ; then

The logic here is that if we remove every character in the line that's not a digit, then we can test to see if the line is still intact (meaning it's a line with just a numeric value) and then the secondary test, to see if it's greater than zero.

That shows how to test to see if an unknown line is a numeric value greater than zero, but what about going back two lines too? That's not too bad:

while read input ; do
  twoback="$oneback"
  oneback="$last"
  last="$input"
done < $in

See how that works? Just keep track of two previous lines...

Here's the entire script:

#!/bin/sh

in="datafile"

twoback="" ; oneback=""

while read input
do
  twoback="$oneback" ; oneback="$last"
  lineWithoutDigits="$(echo $input | sed 's/[^0-9]//g')"
  if [ "$lineWithoutDigits" = "$input" ] ; then
    if [ $input -gt 0 ] ; then
      echo "$twoback"
      echo "$oneback"
    fi
  fi
  last="$input"
done < $in
exit 0

So there's your answer. I think. :-)



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments
Rather amazingly, there are no comments on this article yet.

I have something to say, now that you mention it, but ...
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you for all your efforts on this Web site by buying you a cup of coffee!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.








Ask Dave Taylor: The iPhone App: Advertisement



Follow me on Twitter @DaveTaylor

Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Book Links
© 2002 - 2010 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.