The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
need help cutting consecutive lines with sed or awk raghin Shell Programming and Scripting 2 06-04-2008 01:59 AM
How to capture 2 consecutive rows when a condition is true ? Raynon Shell Programming and Scripting 37 10-02-2007 08:26 PM
grab next consecutive line or lines wisher115 Shell Programming and Scripting 1 12-07-2006 04:01 AM
Appending Consecutive lines pondlife Shell Programming and Scripting 8 08-13-2004 02:36 AM
Cutting n consecutive lines from a file... Vishnu UNIX for Dummies Questions & Answers 2 10-18-2002 07:49 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-23-2008
Registered User
 

Join Date: Jul 2008
Posts: 2
consecutive row comparison in awk (?)

hi, I'm totally new to this forum and to awk.

I have what I thought was a simple problem, but I can't get anything to work. Here is an example input file:

3.85 4018.4
3.9 4068.4
3.95 4082.9
4 4099.7

# Property:.......etc.
0 4733.3
0.05 4659.7
0.1 4585.6
0.15 4466.2

Two fields separated at arbitrary positions by blank lines and label headers.

I want to compare the differences between $2 for all consecutive rows but only within each block delimited by the header: i.e.
ABS(4733.3 - 4659.7),
ABS(4659.7 - 4585.6),
but not:
ABS(4099.7 - 4733.3), etc.

I want to record the largest difference (and the location!).

Thanks for any help,
Oscar
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-23-2008
Registered User
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 445
Something like this?

Code:
awk '
        /^$/ { next }
        /^#/ { print maxdiffrec, maxdiff; print; prev=""; maxdiff=0; next }
        prev != "" {
                absdiff=prev>$2 ? prev-$2 : $2-prev
                if (absdiff > maxdiff) { maxdiff=absdiff; maxdiffrec=NR }
        }
        { prev=$2 }
        END { print maxdiffrec, maxdiff }
'
Reply With Quote
  #3 (permalink)  
Old 07-24-2008
Registered User
 

Join Date: Jul 2008
Posts: 2
!

dude! you're my saviour!
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0