consecutive row comparison in awk (?)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting consecutive row comparison in awk (?)
# 1  
Old 07-23-2008
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
# 2  
Old 07-23-2008
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 }
'

# 3  
Old 07-24-2008
!

dude! you're my saviour!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Consecutive count and reset in awk

I have the following file: A1 4.5807 6.4202 B1 2.5704 11.4414 C1 5.5607 5.28872 D1 3.5807 8.2132 E1 3.2206 9.13153 F1 3.0907 9.51532 G1 3.2707 8.99165 H1 2.4607 11.9515 A2 2.5505 11.5307 B2 2.3106 12.7279 C2 3.8507 7.63731 D2 2.6208 11.2214 E2 2.7609 10.652 F2 2.0604 14.2734 G2... (2 Replies)
Discussion started by: Xterra
2 Replies

2. Shell Programming and Scripting

awk print values between consecutive lines

I have a file in below format: file01.txt TERM TERM TERM ABC 12315 68.53 12042013 165144 ABC 12315 62.12 12042013 165145 ABC 12315 122.36 12052013 165146 ABC 12315 582.18 12052013 165147 ABC 12316 2.36 12052013 165141 ABC 12316 ... (8 Replies)
Discussion started by: alex2005
8 Replies

3. Shell Programming and Scripting

select entry from consecutive line awk

Hi folks, I have a file with four columns that looks like the following (tab separated) 1 1 1 a 2 2 2 b 3 3 3 c 4 4 4 d I would like to create a file with always 4 columns but where the third entry corresponds to the thirst entry of the next line and so on, to get the following 1 1 2 a... (4 Replies)
Discussion started by: klebsiella
4 Replies

4. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

5. Shell Programming and Scripting

AWK: combining consecutive values in a field

Hi, Here is my sample input X 2 AAA Y 3 BBB Y 2 CCC Z 4 DDD In field 1, if the value of one line is same as that of next line, I want to concatenate the corresponding value of the second line in the third field with the value of the third field of first line. And I dont need the third... (2 Replies)
Discussion started by: polsum
2 Replies

6. Shell Programming and Scripting

row Comparison

(5 Replies)
Discussion started by: number10
5 Replies

7. Shell Programming and Scripting

awk command : row by row merging of two files

I want to write a scrpit to merge files row wise (actually concatinating) main.txt X Y Z file 1 A B C file 2 1 2 3 now i want the script to check if the file1 is empty or not, if empty then make it like A B C 1 2 3 again to check if second file is empty if not do as done... (0 Replies)
Discussion started by: shashi792
0 Replies

8. Shell Programming and Scripting

How to do row comparison in shell script

Hi, I need help on doing the below thing in shell script. I have a file with millions of rows called "abc.txt". i have another file with millions of rows called "xyz.txt". I would like to do the below operation. Open the abc.txt, read the first line, do some operations on the column... (2 Replies)
Discussion started by: informsrini
2 Replies

9. Shell Programming and Scripting

Sequential comparison (one row with file and so on)

Dear linux experts, I'd like to ask for your support, I've read some posts in this forum about files comparison but haven't found what I'm looking for. I need to create a sequential script to compare row-by-row one file with 34 similar files but without success so far. This is what I get: ... (2 Replies)
Discussion started by: Gery
2 Replies

10. Shell Programming and Scripting

extracting multiple consecutive columns using awk

Hello, I have a matrix 200*10,000 and I need to extract the columns between 40 and 77. I dont want to write in awk all the columns. eg: awk '{print $40, $41, $42,$43 ... $77}'. I think should exist a better way to do this. (10 Replies)
Discussion started by: auratus42
10 Replies
Login or Register to Ask a Question