比较awk中的连续行 [英] comparing consecutive rows in awk

查看:88
本文介绍了比较awk中的连续行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用awk命令比较文件中的连续行.这是输入和输出的示例.

I would like to compare consecutive rows in a file using awk command. Here is an example of input and output.

输入文件

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  

基本上,我想比较当前行是否大于上一行,并继续打印前一行和当前行.当前行小于前一行时,请在随后的每行中添加一个值(10).

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.

推荐答案

已根据注释中的请求进行了更新:

awk '{if ($1<prev) {offset++}; print ($1+(offset*10)); prev=$1}' input_file

现在,每次在连续的行之间从较大的数字变为较小的数字时,这将使增量偏移增加10.

This will now increase the increment offset by 10 each time you go from a larger number to a smaller number between consecutive lines.

这篇关于比较awk中的连续行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆