5

I am trying to collapse sequential numbers to ranges in bash. For example, if my input file is

1
2
3
4
15
16
17
18
22
23
45
46
47

I want the output as:

1 4
15 18
22 23
45 47

How can I do this with awk or sed in a single line command?

Thanks for any help!

dan1st
  • 12,568
  • 8
  • 34
  • 67
arnstrm
  • 379
  • 3
  • 13
  • [This answer](http://stackoverflow.com/a/25727477/258523) of mine might be helpful in giving you the general sort of idea. – Etan Reisner Nov 07 '14 at 20:40
  • oops! thanks for catching that. I edited my question! Sorry, @EtanReisner, I couldn't figure out how to print 2 different lines in a single line from that link. – arnstrm Nov 07 '14 at 20:40
  • How do you want to treat a _singular_ number `1 2 3 4 8 20 21 22 23 24`? – gboffi Nov 07 '14 at 21:23
  • @gboffi the data always occurs as pairs, so, 8 wouldn't exist (in the above string). – arnstrm Nov 07 '14 at 21:32

1 Answers1

10
$ awk 'NR==1{first=$1;last=$1;next} $1 == last+1 {last=$1;next} {print first,last;first=$1;last=first} END{print first,last}' file
1 4
15 18
22 23
45 47

Explanation

  • NR==1{first=$1;last=$1;next}

    On the first line, initialize the variables first and last and skip to next line.

  • $1 == last+1 {last=$1;next}

    If this line continues in the sequence from the last, update last and jump to the next line.

  • print first,last;first=$1;last=first

    If we get here, we have a break in the sequence. Print out the range for the last sequence and reinitialize the variables for a new sequence.

  • END{print first,last}

    After we get to the end of the file, print the final sequence.

John1024
  • 109,961
  • 14
  • 137
  • 171
  • Thanks a lot @John1024 ! I really appreciate your effort explaining it to me! – arnstrm Nov 07 '14 at 21:01
  • 3
    With regard to my comment to the OP question (i.e., a single 8 among the sequences), your code prints `8 8` and from my personal POV it's the right thing to do. – gboffi Nov 07 '14 at 21:41