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
What I want is a list of the gaps
1-4 9-14 18-24
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.
Click the first cell in an empty column, hold the "Shift" key and click the last data cell in that column. This selects all cells between those two points. You need at least as many cells selected as the anticipated number of missing values.
With awk :
awk '$1!=p+1{print p+1"-"$1-1}{p=$1}' file.txt
$1
is the first column from current input linep
is the previous value of the last line($1!=p+1)
is a condition : if $1
is different than previous value +1, then :{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 columninteresting question.
sputnick's awk one-liner is nice. I cannot write a simpler one than his. I just add another way using diff:
seq $(tail -1 file)|diff - file|grep -Po '.*(?=d)'
the output with your example would be:
1,4 9,14 18,24
I knew that there is comma in it, instead of -
. you could replace the grep with sed to get -
, grep cannot change the input text... but the idea is same.
hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With