简体   繁体   English

在这种情况下如何使用Awk

[英]How to use Awk in this situation

how to change this file 如何更改此文件

335
339
666665
666668

to this result 为了这个结果

335
336
337
338
339
666665
666666
666667
666668

explain : between two numbers with the same long, it will push the missed number to make numeric ascending order .Many Thanks 解释:在两个长相同的数字之间,它将把错过的数字按数字升序排列。非常感谢

I believe this does what you want. 我相信这是您想要的。

awk 'alen==length($1) {for (i=a;i<=$1;i++) print i}; {a=$1; alen=length(a); if (a==(i-1)) {a++}}'

When alen (the length of a) is the same as the length of the current line loop between a and $1 printing out all missing values. alen (a的长度)与a$1之间当前行循环的长度相同时,将打印出所有缺失值。

Then set a to the new $1 , alen to the length of a , and when we dealt with a missing range (when a is the same as i - 1 ) increment a so we don't duplicate that number (this handles cases of sequential lines like 335 , 339 , 350 without duplicating 339 ). 然后设置a新的$1alen到的长度a ,当我们处理了一个失踪的范围(当a是相同的i - 1 )增加一个,所以我们不重复这个数字(这个句柄连续的情况下,线像335339350 ,而无需复制339 )。

With credit to @fedorqui for the basic idea. 归功于@fedorqui的基本思想。

Edit: I believe this fixes the problem I noted in the comments (which I think is what @JohnB was indicating as well): 编辑:我相信这可以解决我在评论中指出的问题(我认为这也是@JohnB指出的问题):

awk '{f=0; if (alen==length($1)) {for (i=a;i<=$1;i++) print i} else {f=1}} {a=$1; alen=length(a)} a==(i-1){a++} f{print; a++}'

I feel like there should be a simpler way to do that but I don't see it at the moment. 我觉得应该有一种更简单的方法来做到这一点,但目前还看不到。

Edit again: The input file I ended up testing with: 再次编辑:我最终测试的输入文件为:

335
339
340
345
3412
34125
666665
666668

The first approach is this: 第一种方法是这样的:

$ awk 'NR%2 {a=$1; next} $1>a {for (i=a;i<=$1;i++) print i}' file
335
336
337
338
339
666665
666666
666667
666668

It can be improved as much as info and effort you put in your question :) 可以改善您在问题中投入的信息和精力:)

Explanation 说明

  • NR%2 {a=$1; next} NR%2 {a=$1; next} as NR stands for n umber of r ecord (number of line in this case), NR%2 is 1 if NR is not multiple of 2. So this stores the value of the line in the variable a in the odd lines. NR%2 {a=$1; next}作为NR代表n的棕土r的eCord(线在这种情况下数), NR%2是1,如果NR没有倍数2。所以这个存储在变量中的线的值a在奇数行。 Then, next stops processing current line. 然后, next停止处理当前行。
  • $1>a {for (i=a;i<=$1;i++) print i} in the other cases (even lines), if the value is bigger than the one that was stored, it loops from that value up to the current one, printing all the values in between. $1>a {for (i=a;i<=$1;i++) print i}在其他情况下(偶数行),如果该值大于存储的值,则从该值循环到当前值一,打印之间的所有值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM