Reputation: 525
I would like to compare consecutive rows in a file using awk command. Here is an example of input and output.
Input file
6
7
8
9
10
4
5
6
The output file I wanted to have is like this:
6
7
8
9
10
14
15
16
Basically I wanted to compare if the current line is greater than the previous one and continue to print both the previous and the current lines. As soon as the current line is less than the previous one, add a value (10) to each of the subsequent lines.
Upvotes: 3
Views: 15743
Reputation: 47267
Updated as per request in comment:
awk '{if ($1<prev) {offset++}; print ($1+(offset*10)); prev=$1}' input_file
This will now increase the increment offset by 10 each time you go from a larger number to a smaller number between consecutive lines.
Upvotes: 5
Reputation: 753575
You would have to save the prior value in a variable and compare with that:
awk '{ if ($0 < old) { $0 += 10; } print; old = $0; }'
Upvotes: 1