Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

linux - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

With credit to @fedorqui for the basic idea.

Edit: 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...