
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 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
Categorized:
Shell Script Programming
(Article 8390)
Tagged: hacking linux, shell script programming, shell scripting Previous: What's the history of July 4th, Independence Day? Next: How do I get bloggers to write about our research results? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.
Rather amazingly, there are no comments on this article yet.
I have a lot to say, but ...
I do have a comment, now that you mention it!
|
![]()
Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|