如何用awk在这种情况下, [英] How to use Awk in this situation

查看:120
本文介绍了如何用awk在这种情况下,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改此文件

335
339
666665
666668

这个结果。

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++}}'

阿伦是一样的当前行环路之间的长度(的长度) A $ 1 打印出所有缺失值。

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.

然后设置 A $ 1 阿伦 A ,当我们处理缺失范围时( A 相同<$的长度C $ C> I - 1 )递增,所以我们不要重复这个数字(这个处理顺序线情况下,像 335 339 350 ,而无需复制 339 )。

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).

随着信贷@fedorqui的基本思想。

With credit to @fedorqui for the basic idea.

编辑:我相信这个解决我在评论中所指出的问题(我认为这是什么@JohnB被显示为好):

I believe this fixes the problem I noted in the comments (which I think is what @JohnB was indicating as well):

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

这篇关于如何用awk在这种情况下,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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