如何用Awk提取第三列下一行值增大时的当前行?
Solution to Extract Lines Where Next Line's 3rd Column Increases
Got it, let's break down what you need and fix this. Your original awk script awk '$3==last{next} {last=$3} 1' works for removing consecutive duplicate lines based on the 3rd column, but it doesn't address your actual requirement: outputting a line only when the next line's 3rd column value is larger than the current line's, plus including the final line of the input.
Correct Awk Script
Here's the script that will produce your desired output:
NR == 1 { prev_line = $0; prev_3 = $3; next } { if ($3 > prev_3) { print prev_line } prev_line = $0 prev_3 = $3 } END { print prev_line }
How It Works
Let's walk through the logic step by step:
- Handle the first line: We store the entire first line (
prev_line) and its 3rd column value (prev_3), then skip to the next line since we can't compare it to a previous line yet. - Process every subsequent line:
- Compare the current line's 3rd column to the stored value from the previous line.
- If the current line's 3rd column is larger, we print the stored previous line (this is the line we want to keep, since the next line's value increased).
- Update our stored line and 3rd column value to the current line for the next iteration.
- Final line: In the
ENDblock, we print the last line of the input, since there's no next line to compare it to and your desired output includes this line.
Testing with Your Input
When you run this script against your sample input, it will output exactly the lines you expect:
11* 3 0.3 1.01091 20.8 14* 4 0.9 1.00553 0 18* 6 1.4 1.01334 25 21* 7 1.8 1.00832 8.3 24* 8 2.4 1.00443 0
内容的提问来源于stack exchange,提问作者Mohd
相关产品推荐
相关产品推荐

