Finding gaps in sequential numbers

后端 未结 6 1079
粉色の甜心
粉色の甜心 2020-12-05 09:31

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 tip

6条回答
  •  情深已故
    2020-12-05 10:25

    With awk :

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

    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

提交回复
热议问题