Find the answer to your Linux question:
Results 1 to 6 of 6
Hi All, I have some numbers in a file as : 2 8 9 10 14 19 21 22 23 27 I want, if they 2 or more numbers are ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2013
    Location
    Gurgaon, India
    Posts
    6

    Need a script to make a number series


    Hi All,

    I have some numbers in a file as :
    2
    8
    9
    10
    14
    19
    21
    22
    23
    27

    I want, if they 2 or more numbers are consecutive numbers, they should for a series like :
    8-10
    21-23

    Thanks in advance.

    Regards
    Deepanshu Madan

  2. #2
    Linux Engineer docbop's Avatar
    Join Date
    Nov 2009
    Location
    Woodshed, CA
    Posts
    905
    Looks like a homework assignment... So how do you think you will approach this? What types of commands do you think you will need? What have you tried so far?
    A lion does not lose sleep, over the opinion of sheep.

  3. #3
    Just Joined!
    Join Date
    Apr 2013
    Location
    Gurgaon, India
    Posts
    6


    Noh, Not a homework,!! Looking to optimize few things on my current project thought to use, some expert's suggestions.

    I have few while (read line) loop's idea in my mind.

  4. #4
    Linux User
    Join Date
    Dec 2011
    Location
    Turtle Island West
    Posts
    345
    What if the numbers in the source file are not sequential, or duplicated? Or not numbers at all? Hexadecimal? Octal?

    I'd read the source file, convert the lines into valid numbers, 0 if not valid, and create an array n[] that corresponds to each number read.

    Then I'd run through the array and find the sequences.

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,454
    Homework or not, show the code you have tried to create, and we might decide to help you. It still sounds like homework to me...
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  6. #6
    Linux Newbie
    Join Date
    Mar 2014
    Posts
    119
    I want, if they 2 or more numbers are consecutive numbers, they should for a series like :
    8-10
    21-23
    I have no idea what that means.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •