![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
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 }
'
|