寻找序号中的空白 [英] Finding gaps in sequential numbers

查看:61
本文介绍了寻找序号中的空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是为了谋生而做这些事情,因此,如果这是一个简单的问题(或者比我想的要复杂的多),请原谅我.我一直在浏览档案,发现了一些很接近的技巧,但是作为一个新手,我不确定如何调整自己的需求,或者它们超出了我的理解范围.

I don’t do this stuff for a living so forgive me if it’s a simple question (or more complicated than I think). I‘ve been digging through the archives and found a lot of tips that are close but being a novice I’m not sure how to tweak for my needs or they are way beyond my understanding.

我有一些大的数据文件,我可以解析这些文件以生成大多数是连续的坐标列表

I have some large data files that I can parse out to generate a list of coordinate that are mostly sequential

5
6
7
8
15
16
17
25
26
27

我想要的是差距列表

1-4
9-14
18-24

我不知道 perl SQL 或其他任何奇特的东西,但我认为我可以做一些可以从下一个减去一个数字的事情.然后,我至少可以将差异不为 1 -1 的输出grep并使用该输出来获得差距.

I don’t know perl, SQL or anything fancy but thought I might be able to do something that would subtract one number from the next. I could then at least grep the output where the difference was not 1 or -1 and work with that to get the gaps.

推荐答案

带有:

awk '$1!=p+1{print p+1"-"$1-1}{p=$1}' file.txt

解释

  • $1是当前输入行的第一列
  • p是最后一行的上一个值
  • 所以($1!=p+1)是一个条件:如果$1与先前的值+1不同,则:
  • 执行此部分:{print p+1 "-" $1-1}:打印先前的值+1,-字符和第一列+ 1
  • 每行执行
  • {p=$1}:将p分配给当前的第一列
  • explanations

    • $1 is the first column from current input line
    • p is the previous value of the last line
    • so ($1!=p+1) is a condition : if $1 is different than previous value +1, then :
    • this part is executed : {print p+1 "-" $1-1} : print previous value +1, the - character and fist columns + 1
    • {p=$1} is executed for each lines : p is assigned to the current 1st column
    • 这篇关于寻找序号中的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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