作者:herozhx | 来源:互联网 | 2023-05-19 05:28
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
解释:在两个长度相同的数字之间,它会将错过的数字推到数字升序。很多谢谢
2 个解决方案
6
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
).
然后设置一个新的$ 1,alen到a的长度,当我们处理一个缺失的范围(当a与i-1相同时)增加a所以我们不重复那个数字(这处理顺序的情况)像335,339,350这样的行没有重复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