Hexadecimal number sequence checker in Linux?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have a list of hexadecimal number. I would like to check whether they are in sequence or not. That is, they should be consecutive numbers, in increasing order. In other words, there should be an increment of 1 from each line to the next.



Sample list of hexadecimal numbers:



85AF
85B0
85B1
85B2
85B3
85B4
85B5
85B6
85B7
85B8
85B9
85BA
85BB
85BC
85BD
85BE
85BF
85C0


In reality I would have more than 500 numbers to check through.



Desired output:



All numbers are in sequence
(or)
Numbers are not in sequence.


This is on Solaris, with ksh.










share|improve this question



























    up vote
    1
    down vote

    favorite












    I have a list of hexadecimal number. I would like to check whether they are in sequence or not. That is, they should be consecutive numbers, in increasing order. In other words, there should be an increment of 1 from each line to the next.



    Sample list of hexadecimal numbers:



    85AF
    85B0
    85B1
    85B2
    85B3
    85B4
    85B5
    85B6
    85B7
    85B8
    85B9
    85BA
    85BB
    85BC
    85BD
    85BE
    85BF
    85C0


    In reality I would have more than 500 numbers to check through.



    Desired output:



    All numbers are in sequence
    (or)
    Numbers are not in sequence.


    This is on Solaris, with ksh.










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a list of hexadecimal number. I would like to check whether they are in sequence or not. That is, they should be consecutive numbers, in increasing order. In other words, there should be an increment of 1 from each line to the next.



      Sample list of hexadecimal numbers:



      85AF
      85B0
      85B1
      85B2
      85B3
      85B4
      85B5
      85B6
      85B7
      85B8
      85B9
      85BA
      85BB
      85BC
      85BD
      85BE
      85BF
      85C0


      In reality I would have more than 500 numbers to check through.



      Desired output:



      All numbers are in sequence
      (or)
      Numbers are not in sequence.


      This is on Solaris, with ksh.










      share|improve this question















      I have a list of hexadecimal number. I would like to check whether they are in sequence or not. That is, they should be consecutive numbers, in increasing order. In other words, there should be an increment of 1 from each line to the next.



      Sample list of hexadecimal numbers:



      85AF
      85B0
      85B1
      85B2
      85B3
      85B4
      85B5
      85B6
      85B7
      85B8
      85B9
      85BA
      85BB
      85BC
      85BD
      85BE
      85BF
      85C0


      In reality I would have more than 500 numbers to check through.



      Desired output:



      All numbers are in sequence
      (or)
      Numbers are not in sequence.


      This is on Solaris, with ksh.







      shell shell-script text-processing ksh arithmetic






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 18 '15 at 20:51









      Gilles

      512k12010151547




      512k12010151547










      asked May 18 '15 at 17:18









      ayrton_senna

      5072418




      5072418




















          6 Answers
          6






          active

          oldest

          votes

















          up vote
          2
          down vote













          Awk could do this easily if the output was in decimal, but it can't parse hexadecimal numbers (at least standard awk can't, some versions such as GNU awk can). You can use bc to do the conversion. This works on all POSIX systems.



           echo "ibase=16"; cat input.txt; | bc |
          awk 'NR==1 origin = $0-1
          $0!=origin+NR print "Out-of-sequence number at line", NR; exit(1)' >&2





          share|improve this answer



























            up vote
            2
            down vote













            As said in another answer, it would be nice to do this in awk, but ancient awk (the one probably available in Solaris) doesn't understand hex numbers.



            A fast solution is to use wc -l and some ksh math:



            #! /bin/ksh

            first="0x$(head -n1 infile)"
            last="0x$(tail -n1 infile)"
            lines=$(wc -l <infile)

            if [[ "$(( last - first + 1 - lines ))" -eq 0 ]]; then
            echo "All numbers are in sequence."
            else
            echo "Numbers are not in sequence."
            fi





            share|improve this answer



























              up vote
              1
              down vote













              Here is one option:



              while read x; do echo $((16#$x)); done <yourfile | awk 's && $1!=s+1exit(1)s=$1'


              This shell command will produce an exit status of 1 (on fail) and 0 (on success). This command can be used e.g. in an if-clause like the following to produce the desired output:



              if while read x; do echo $((16#$x)); done < yourfile | awk 's && $1!=s+1exit(1)s=$1'
              then echo All numbers are in sequence
              else echo Numbers are not in sequence
              fi


              (Note that you have you substitute the name "yourfile" by the file name that contains your hex number sequence.)



              You can also omit the shell loop and use GNU awk's option -n to process the hex numbers directly:



              if sed 's/^/0x/' <yourfile | awk -n 's && $1+0!=s+1exit(1)s=$1+0'
              then echo All numbers are in sequence
              else echo Numbers are not in sequence.
              fi


              Note: sed is used here to create the syntactically expected format for the hex numbers (with leading 0x).






              share|improve this answer






















              • this method seems to be nice. But i can not put it in a script. awk is bailing out with a syntax error. Can you provide me the .KSH file.
                – ayrton_senna
                May 18 '15 at 18:46










              • Which of the two versions are you trying? And are you working on solaris? - In the latter case see whether you have a GNU awk installed. Or else try the first version with /usr/xpg4/bin/awk (instead of the default awk). - WRT the requested "ksh file"; you would just copy/paste the posted code in a file testhexseq (don't forget to adjust the name of the hex source file), and call it as ksh testhexseq.
                – Janis
                May 18 '15 at 18:48


















              up vote
              0
              down vote













              You can use sort with the option -c to check if the input data is sorted:




              -c



              Check that the single input file is ordered as specified by the arguments and
              the collating sequence of the current locale. Output
              shall not be sent to standard output. The exit code shall indicate
              whether or not disorder was detected or an error occurred. If
              disorder (or, with -u, a duplicate key) is detected, a warning
              message shall be sent to standard error indicating where the disorder
              or duplicate key was found.




              So a simple



              LC_ALL=C 
              sort -c data 2> /dev/null
              && echo All numbers are in sequence
              || echo Numbers are not in sequence


              would do.






              share|improve this answer




















              • Do not work with trailing empty lines. Just remove it and use <(sed '/S/!d' data) instead data
                – Costas
                May 18 '15 at 19:26










              • @Costas OP doesn't say anything about trailing lines.
                – FloHimself
                May 18 '15 at 19:31










              • This doesn't check that the increment is 1 (as clarified in a comment prior to your answer).
                – Gilles
                May 18 '15 at 20:51










              • This wasn't a requirement in OP before your edit.
                – FloHimself
                May 19 '15 at 4:58

















              up vote
              0
              down vote













              If you'd like strict sequence it can be easy done by produce etalon sequence and compare it with file



              [ $(comm --nocheck-order -3 
              <(sed '/S/!d' file)
              <(printf "%Xn"
              $(seq $((16#$(sed '/./!d;q' file)))
              $((16#$(tac file|sed '/./!d;q')))))) ] &&
              echo 'Numbers are not in sequence' ||
              echo 'All numbers are in sequence'


              Just to check if all numbers are hexadecimal:



              sed '/^[[:xdigit:] ]*$/!
              s/.*/Numbers are not in sequence: &/
              q

              $! d
              s/.*/All numbers are in sequence/' hexadecimal_number.list



              • /^[[:xdigit:] ]*$/ check if in line there are hexadecimal symbols and space only (or nothing - empty line)


              • ! reverse match, so execute commands in just if there some other symbols than described above


              • s/.*/.../;q substitute all line by Numbers are not in sequence than quit script with printing line


              • $! d if script didn't execute previous (pattern match) and if line is not last delete it it start with next line


              • s/.*/.../ if script reach last line (so all lines match pattern) substitute last line by All numbers are in sequence an print it





              share|improve this answer






















              • should i put this in a script and run it or its a single line command? i am newbie, sorry for my ignorance.
                – ayrton_senna
                May 18 '15 at 18:02










              • @ayrton_senna; Typically longer commands are put in a file (say yourscript) and executed by calling the file with a shell (e.g. bash yourscript). - But in this case I don't see that this code works - it certainly doesn't work when I tried - nor do I see how it would do the desired task. (Costas may want to explain it.)
                – Janis
                May 18 '15 at 18:17











              • @ayrton_senna Up to you. It can be used in all variants.
                – Costas
                May 18 '15 at 18:18










              • @Janis I have tested - it work
                – Costas
                May 18 '15 at 18:21










              • @Costas; If I remove one of the lines in the data file, or add a data element AAAA - in both cases disturbing the order -, I still get a positive result, even though it shouldn't. - Are you sure you tested those cases?
                – Janis
                May 18 '15 at 18:33


















              up vote
              0
              down vote













              Given the input file hexlist.txt, this works in ksh and bash. It uses the numinterval util. If numinterval isn't available, uncomment the faker function:



              # fake `numinterval` function
              # numinterval() dc ;

              n=; eval printf '%s\n' $(printf '$((16#%sn)) ' $(<hexlist.txt)) |
              numinterval | grep -qw 1 || n=not ; echo All numbers are $n in sequence





              share|improve this answer




















                Your Answer







                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "106"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f204138%2fhexadecimal-number-sequence-checker-in-linux%23new-answer', 'question_page');

                );

                Post as a guest






























                6 Answers
                6






                active

                oldest

                votes








                6 Answers
                6






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                2
                down vote













                Awk could do this easily if the output was in decimal, but it can't parse hexadecimal numbers (at least standard awk can't, some versions such as GNU awk can). You can use bc to do the conversion. This works on all POSIX systems.



                 echo "ibase=16"; cat input.txt; | bc |
                awk 'NR==1 origin = $0-1
                $0!=origin+NR print "Out-of-sequence number at line", NR; exit(1)' >&2





                share|improve this answer
























                  up vote
                  2
                  down vote













                  Awk could do this easily if the output was in decimal, but it can't parse hexadecimal numbers (at least standard awk can't, some versions such as GNU awk can). You can use bc to do the conversion. This works on all POSIX systems.



                   echo "ibase=16"; cat input.txt; | bc |
                  awk 'NR==1 origin = $0-1
                  $0!=origin+NR print "Out-of-sequence number at line", NR; exit(1)' >&2





                  share|improve this answer






















                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    Awk could do this easily if the output was in decimal, but it can't parse hexadecimal numbers (at least standard awk can't, some versions such as GNU awk can). You can use bc to do the conversion. This works on all POSIX systems.



                     echo "ibase=16"; cat input.txt; | bc |
                    awk 'NR==1 origin = $0-1
                    $0!=origin+NR print "Out-of-sequence number at line", NR; exit(1)' >&2





                    share|improve this answer












                    Awk could do this easily if the output was in decimal, but it can't parse hexadecimal numbers (at least standard awk can't, some versions such as GNU awk can). You can use bc to do the conversion. This works on all POSIX systems.



                     echo "ibase=16"; cat input.txt; | bc |
                    awk 'NR==1 origin = $0-1
                    $0!=origin+NR print "Out-of-sequence number at line", NR; exit(1)' >&2






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 18 '15 at 23:30









                    Gilles

                    512k12010151547




                    512k12010151547






















                        up vote
                        2
                        down vote













                        As said in another answer, it would be nice to do this in awk, but ancient awk (the one probably available in Solaris) doesn't understand hex numbers.



                        A fast solution is to use wc -l and some ksh math:



                        #! /bin/ksh

                        first="0x$(head -n1 infile)"
                        last="0x$(tail -n1 infile)"
                        lines=$(wc -l <infile)

                        if [[ "$(( last - first + 1 - lines ))" -eq 0 ]]; then
                        echo "All numbers are in sequence."
                        else
                        echo "Numbers are not in sequence."
                        fi





                        share|improve this answer
























                          up vote
                          2
                          down vote













                          As said in another answer, it would be nice to do this in awk, but ancient awk (the one probably available in Solaris) doesn't understand hex numbers.



                          A fast solution is to use wc -l and some ksh math:



                          #! /bin/ksh

                          first="0x$(head -n1 infile)"
                          last="0x$(tail -n1 infile)"
                          lines=$(wc -l <infile)

                          if [[ "$(( last - first + 1 - lines ))" -eq 0 ]]; then
                          echo "All numbers are in sequence."
                          else
                          echo "Numbers are not in sequence."
                          fi





                          share|improve this answer






















                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            As said in another answer, it would be nice to do this in awk, but ancient awk (the one probably available in Solaris) doesn't understand hex numbers.



                            A fast solution is to use wc -l and some ksh math:



                            #! /bin/ksh

                            first="0x$(head -n1 infile)"
                            last="0x$(tail -n1 infile)"
                            lines=$(wc -l <infile)

                            if [[ "$(( last - first + 1 - lines ))" -eq 0 ]]; then
                            echo "All numbers are in sequence."
                            else
                            echo "Numbers are not in sequence."
                            fi





                            share|improve this answer












                            As said in another answer, it would be nice to do this in awk, but ancient awk (the one probably available in Solaris) doesn't understand hex numbers.



                            A fast solution is to use wc -l and some ksh math:



                            #! /bin/ksh

                            first="0x$(head -n1 infile)"
                            last="0x$(tail -n1 infile)"
                            lines=$(wc -l <infile)

                            if [[ "$(( last - first + 1 - lines ))" -eq 0 ]]; then
                            echo "All numbers are in sequence."
                            else
                            echo "Numbers are not in sequence."
                            fi






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Sep 28 at 1:40









                            Isaac

                            7,60511137




                            7,60511137




















                                up vote
                                1
                                down vote













                                Here is one option:



                                while read x; do echo $((16#$x)); done <yourfile | awk 's && $1!=s+1exit(1)s=$1'


                                This shell command will produce an exit status of 1 (on fail) and 0 (on success). This command can be used e.g. in an if-clause like the following to produce the desired output:



                                if while read x; do echo $((16#$x)); done < yourfile | awk 's && $1!=s+1exit(1)s=$1'
                                then echo All numbers are in sequence
                                else echo Numbers are not in sequence
                                fi


                                (Note that you have you substitute the name "yourfile" by the file name that contains your hex number sequence.)



                                You can also omit the shell loop and use GNU awk's option -n to process the hex numbers directly:



                                if sed 's/^/0x/' <yourfile | awk -n 's && $1+0!=s+1exit(1)s=$1+0'
                                then echo All numbers are in sequence
                                else echo Numbers are not in sequence.
                                fi


                                Note: sed is used here to create the syntactically expected format for the hex numbers (with leading 0x).






                                share|improve this answer






















                                • this method seems to be nice. But i can not put it in a script. awk is bailing out with a syntax error. Can you provide me the .KSH file.
                                  – ayrton_senna
                                  May 18 '15 at 18:46










                                • Which of the two versions are you trying? And are you working on solaris? - In the latter case see whether you have a GNU awk installed. Or else try the first version with /usr/xpg4/bin/awk (instead of the default awk). - WRT the requested "ksh file"; you would just copy/paste the posted code in a file testhexseq (don't forget to adjust the name of the hex source file), and call it as ksh testhexseq.
                                  – Janis
                                  May 18 '15 at 18:48















                                up vote
                                1
                                down vote













                                Here is one option:



                                while read x; do echo $((16#$x)); done <yourfile | awk 's && $1!=s+1exit(1)s=$1'


                                This shell command will produce an exit status of 1 (on fail) and 0 (on success). This command can be used e.g. in an if-clause like the following to produce the desired output:



                                if while read x; do echo $((16#$x)); done < yourfile | awk 's && $1!=s+1exit(1)s=$1'
                                then echo All numbers are in sequence
                                else echo Numbers are not in sequence
                                fi


                                (Note that you have you substitute the name "yourfile" by the file name that contains your hex number sequence.)



                                You can also omit the shell loop and use GNU awk's option -n to process the hex numbers directly:



                                if sed 's/^/0x/' <yourfile | awk -n 's && $1+0!=s+1exit(1)s=$1+0'
                                then echo All numbers are in sequence
                                else echo Numbers are not in sequence.
                                fi


                                Note: sed is used here to create the syntactically expected format for the hex numbers (with leading 0x).






                                share|improve this answer






















                                • this method seems to be nice. But i can not put it in a script. awk is bailing out with a syntax error. Can you provide me the .KSH file.
                                  – ayrton_senna
                                  May 18 '15 at 18:46










                                • Which of the two versions are you trying? And are you working on solaris? - In the latter case see whether you have a GNU awk installed. Or else try the first version with /usr/xpg4/bin/awk (instead of the default awk). - WRT the requested "ksh file"; you would just copy/paste the posted code in a file testhexseq (don't forget to adjust the name of the hex source file), and call it as ksh testhexseq.
                                  – Janis
                                  May 18 '15 at 18:48













                                up vote
                                1
                                down vote










                                up vote
                                1
                                down vote









                                Here is one option:



                                while read x; do echo $((16#$x)); done <yourfile | awk 's && $1!=s+1exit(1)s=$1'


                                This shell command will produce an exit status of 1 (on fail) and 0 (on success). This command can be used e.g. in an if-clause like the following to produce the desired output:



                                if while read x; do echo $((16#$x)); done < yourfile | awk 's && $1!=s+1exit(1)s=$1'
                                then echo All numbers are in sequence
                                else echo Numbers are not in sequence
                                fi


                                (Note that you have you substitute the name "yourfile" by the file name that contains your hex number sequence.)



                                You can also omit the shell loop and use GNU awk's option -n to process the hex numbers directly:



                                if sed 's/^/0x/' <yourfile | awk -n 's && $1+0!=s+1exit(1)s=$1+0'
                                then echo All numbers are in sequence
                                else echo Numbers are not in sequence.
                                fi


                                Note: sed is used here to create the syntactically expected format for the hex numbers (with leading 0x).






                                share|improve this answer














                                Here is one option:



                                while read x; do echo $((16#$x)); done <yourfile | awk 's && $1!=s+1exit(1)s=$1'


                                This shell command will produce an exit status of 1 (on fail) and 0 (on success). This command can be used e.g. in an if-clause like the following to produce the desired output:



                                if while read x; do echo $((16#$x)); done < yourfile | awk 's && $1!=s+1exit(1)s=$1'
                                then echo All numbers are in sequence
                                else echo Numbers are not in sequence
                                fi


                                (Note that you have you substitute the name "yourfile" by the file name that contains your hex number sequence.)



                                You can also omit the shell loop and use GNU awk's option -n to process the hex numbers directly:



                                if sed 's/^/0x/' <yourfile | awk -n 's && $1+0!=s+1exit(1)s=$1+0'
                                then echo All numbers are in sequence
                                else echo Numbers are not in sequence.
                                fi


                                Note: sed is used here to create the syntactically expected format for the hex numbers (with leading 0x).







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited May 18 '15 at 18:29

























                                answered May 18 '15 at 18:14









                                Janis

                                9,81011236




                                9,81011236











                                • this method seems to be nice. But i can not put it in a script. awk is bailing out with a syntax error. Can you provide me the .KSH file.
                                  – ayrton_senna
                                  May 18 '15 at 18:46










                                • Which of the two versions are you trying? And are you working on solaris? - In the latter case see whether you have a GNU awk installed. Or else try the first version with /usr/xpg4/bin/awk (instead of the default awk). - WRT the requested "ksh file"; you would just copy/paste the posted code in a file testhexseq (don't forget to adjust the name of the hex source file), and call it as ksh testhexseq.
                                  – Janis
                                  May 18 '15 at 18:48

















                                • this method seems to be nice. But i can not put it in a script. awk is bailing out with a syntax error. Can you provide me the .KSH file.
                                  – ayrton_senna
                                  May 18 '15 at 18:46










                                • Which of the two versions are you trying? And are you working on solaris? - In the latter case see whether you have a GNU awk installed. Or else try the first version with /usr/xpg4/bin/awk (instead of the default awk). - WRT the requested "ksh file"; you would just copy/paste the posted code in a file testhexseq (don't forget to adjust the name of the hex source file), and call it as ksh testhexseq.
                                  – Janis
                                  May 18 '15 at 18:48
















                                this method seems to be nice. But i can not put it in a script. awk is bailing out with a syntax error. Can you provide me the .KSH file.
                                – ayrton_senna
                                May 18 '15 at 18:46




                                this method seems to be nice. But i can not put it in a script. awk is bailing out with a syntax error. Can you provide me the .KSH file.
                                – ayrton_senna
                                May 18 '15 at 18:46












                                Which of the two versions are you trying? And are you working on solaris? - In the latter case see whether you have a GNU awk installed. Or else try the first version with /usr/xpg4/bin/awk (instead of the default awk). - WRT the requested "ksh file"; you would just copy/paste the posted code in a file testhexseq (don't forget to adjust the name of the hex source file), and call it as ksh testhexseq.
                                – Janis
                                May 18 '15 at 18:48





                                Which of the two versions are you trying? And are you working on solaris? - In the latter case see whether you have a GNU awk installed. Or else try the first version with /usr/xpg4/bin/awk (instead of the default awk). - WRT the requested "ksh file"; you would just copy/paste the posted code in a file testhexseq (don't forget to adjust the name of the hex source file), and call it as ksh testhexseq.
                                – Janis
                                May 18 '15 at 18:48











                                up vote
                                0
                                down vote













                                You can use sort with the option -c to check if the input data is sorted:




                                -c



                                Check that the single input file is ordered as specified by the arguments and
                                the collating sequence of the current locale. Output
                                shall not be sent to standard output. The exit code shall indicate
                                whether or not disorder was detected or an error occurred. If
                                disorder (or, with -u, a duplicate key) is detected, a warning
                                message shall be sent to standard error indicating where the disorder
                                or duplicate key was found.




                                So a simple



                                LC_ALL=C 
                                sort -c data 2> /dev/null
                                && echo All numbers are in sequence
                                || echo Numbers are not in sequence


                                would do.






                                share|improve this answer




















                                • Do not work with trailing empty lines. Just remove it and use <(sed '/S/!d' data) instead data
                                  – Costas
                                  May 18 '15 at 19:26










                                • @Costas OP doesn't say anything about trailing lines.
                                  – FloHimself
                                  May 18 '15 at 19:31










                                • This doesn't check that the increment is 1 (as clarified in a comment prior to your answer).
                                  – Gilles
                                  May 18 '15 at 20:51










                                • This wasn't a requirement in OP before your edit.
                                  – FloHimself
                                  May 19 '15 at 4:58














                                up vote
                                0
                                down vote













                                You can use sort with the option -c to check if the input data is sorted:




                                -c



                                Check that the single input file is ordered as specified by the arguments and
                                the collating sequence of the current locale. Output
                                shall not be sent to standard output. The exit code shall indicate
                                whether or not disorder was detected or an error occurred. If
                                disorder (or, with -u, a duplicate key) is detected, a warning
                                message shall be sent to standard error indicating where the disorder
                                or duplicate key was found.




                                So a simple



                                LC_ALL=C 
                                sort -c data 2> /dev/null
                                && echo All numbers are in sequence
                                || echo Numbers are not in sequence


                                would do.






                                share|improve this answer




















                                • Do not work with trailing empty lines. Just remove it and use <(sed '/S/!d' data) instead data
                                  – Costas
                                  May 18 '15 at 19:26










                                • @Costas OP doesn't say anything about trailing lines.
                                  – FloHimself
                                  May 18 '15 at 19:31










                                • This doesn't check that the increment is 1 (as clarified in a comment prior to your answer).
                                  – Gilles
                                  May 18 '15 at 20:51










                                • This wasn't a requirement in OP before your edit.
                                  – FloHimself
                                  May 19 '15 at 4:58












                                up vote
                                0
                                down vote










                                up vote
                                0
                                down vote









                                You can use sort with the option -c to check if the input data is sorted:




                                -c



                                Check that the single input file is ordered as specified by the arguments and
                                the collating sequence of the current locale. Output
                                shall not be sent to standard output. The exit code shall indicate
                                whether or not disorder was detected or an error occurred. If
                                disorder (or, with -u, a duplicate key) is detected, a warning
                                message shall be sent to standard error indicating where the disorder
                                or duplicate key was found.




                                So a simple



                                LC_ALL=C 
                                sort -c data 2> /dev/null
                                && echo All numbers are in sequence
                                || echo Numbers are not in sequence


                                would do.






                                share|improve this answer












                                You can use sort with the option -c to check if the input data is sorted:




                                -c



                                Check that the single input file is ordered as specified by the arguments and
                                the collating sequence of the current locale. Output
                                shall not be sent to standard output. The exit code shall indicate
                                whether or not disorder was detected or an error occurred. If
                                disorder (or, with -u, a duplicate key) is detected, a warning
                                message shall be sent to standard error indicating where the disorder
                                or duplicate key was found.




                                So a simple



                                LC_ALL=C 
                                sort -c data 2> /dev/null
                                && echo All numbers are in sequence
                                || echo Numbers are not in sequence


                                would do.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered May 18 '15 at 19:01









                                FloHimself

                                6,01421318




                                6,01421318











                                • Do not work with trailing empty lines. Just remove it and use <(sed '/S/!d' data) instead data
                                  – Costas
                                  May 18 '15 at 19:26










                                • @Costas OP doesn't say anything about trailing lines.
                                  – FloHimself
                                  May 18 '15 at 19:31










                                • This doesn't check that the increment is 1 (as clarified in a comment prior to your answer).
                                  – Gilles
                                  May 18 '15 at 20:51










                                • This wasn't a requirement in OP before your edit.
                                  – FloHimself
                                  May 19 '15 at 4:58
















                                • Do not work with trailing empty lines. Just remove it and use <(sed '/S/!d' data) instead data
                                  – Costas
                                  May 18 '15 at 19:26










                                • @Costas OP doesn't say anything about trailing lines.
                                  – FloHimself
                                  May 18 '15 at 19:31










                                • This doesn't check that the increment is 1 (as clarified in a comment prior to your answer).
                                  – Gilles
                                  May 18 '15 at 20:51










                                • This wasn't a requirement in OP before your edit.
                                  – FloHimself
                                  May 19 '15 at 4:58















                                Do not work with trailing empty lines. Just remove it and use <(sed '/S/!d' data) instead data
                                – Costas
                                May 18 '15 at 19:26




                                Do not work with trailing empty lines. Just remove it and use <(sed '/S/!d' data) instead data
                                – Costas
                                May 18 '15 at 19:26












                                @Costas OP doesn't say anything about trailing lines.
                                – FloHimself
                                May 18 '15 at 19:31




                                @Costas OP doesn't say anything about trailing lines.
                                – FloHimself
                                May 18 '15 at 19:31












                                This doesn't check that the increment is 1 (as clarified in a comment prior to your answer).
                                – Gilles
                                May 18 '15 at 20:51




                                This doesn't check that the increment is 1 (as clarified in a comment prior to your answer).
                                – Gilles
                                May 18 '15 at 20:51












                                This wasn't a requirement in OP before your edit.
                                – FloHimself
                                May 19 '15 at 4:58




                                This wasn't a requirement in OP before your edit.
                                – FloHimself
                                May 19 '15 at 4:58










                                up vote
                                0
                                down vote













                                If you'd like strict sequence it can be easy done by produce etalon sequence and compare it with file



                                [ $(comm --nocheck-order -3 
                                <(sed '/S/!d' file)
                                <(printf "%Xn"
                                $(seq $((16#$(sed '/./!d;q' file)))
                                $((16#$(tac file|sed '/./!d;q')))))) ] &&
                                echo 'Numbers are not in sequence' ||
                                echo 'All numbers are in sequence'


                                Just to check if all numbers are hexadecimal:



                                sed '/^[[:xdigit:] ]*$/!
                                s/.*/Numbers are not in sequence: &/
                                q

                                $! d
                                s/.*/All numbers are in sequence/' hexadecimal_number.list



                                • /^[[:xdigit:] ]*$/ check if in line there are hexadecimal symbols and space only (or nothing - empty line)


                                • ! reverse match, so execute commands in just if there some other symbols than described above


                                • s/.*/.../;q substitute all line by Numbers are not in sequence than quit script with printing line


                                • $! d if script didn't execute previous (pattern match) and if line is not last delete it it start with next line


                                • s/.*/.../ if script reach last line (so all lines match pattern) substitute last line by All numbers are in sequence an print it





                                share|improve this answer






















                                • should i put this in a script and run it or its a single line command? i am newbie, sorry for my ignorance.
                                  – ayrton_senna
                                  May 18 '15 at 18:02










                                • @ayrton_senna; Typically longer commands are put in a file (say yourscript) and executed by calling the file with a shell (e.g. bash yourscript). - But in this case I don't see that this code works - it certainly doesn't work when I tried - nor do I see how it would do the desired task. (Costas may want to explain it.)
                                  – Janis
                                  May 18 '15 at 18:17











                                • @ayrton_senna Up to you. It can be used in all variants.
                                  – Costas
                                  May 18 '15 at 18:18










                                • @Janis I have tested - it work
                                  – Costas
                                  May 18 '15 at 18:21










                                • @Costas; If I remove one of the lines in the data file, or add a data element AAAA - in both cases disturbing the order -, I still get a positive result, even though it shouldn't. - Are you sure you tested those cases?
                                  – Janis
                                  May 18 '15 at 18:33















                                up vote
                                0
                                down vote













                                If you'd like strict sequence it can be easy done by produce etalon sequence and compare it with file



                                [ $(comm --nocheck-order -3 
                                <(sed '/S/!d' file)
                                <(printf "%Xn"
                                $(seq $((16#$(sed '/./!d;q' file)))
                                $((16#$(tac file|sed '/./!d;q')))))) ] &&
                                echo 'Numbers are not in sequence' ||
                                echo 'All numbers are in sequence'


                                Just to check if all numbers are hexadecimal:



                                sed '/^[[:xdigit:] ]*$/!
                                s/.*/Numbers are not in sequence: &/
                                q

                                $! d
                                s/.*/All numbers are in sequence/' hexadecimal_number.list



                                • /^[[:xdigit:] ]*$/ check if in line there are hexadecimal symbols and space only (or nothing - empty line)


                                • ! reverse match, so execute commands in just if there some other symbols than described above


                                • s/.*/.../;q substitute all line by Numbers are not in sequence than quit script with printing line


                                • $! d if script didn't execute previous (pattern match) and if line is not last delete it it start with next line


                                • s/.*/.../ if script reach last line (so all lines match pattern) substitute last line by All numbers are in sequence an print it





                                share|improve this answer






















                                • should i put this in a script and run it or its a single line command? i am newbie, sorry for my ignorance.
                                  – ayrton_senna
                                  May 18 '15 at 18:02










                                • @ayrton_senna; Typically longer commands are put in a file (say yourscript) and executed by calling the file with a shell (e.g. bash yourscript). - But in this case I don't see that this code works - it certainly doesn't work when I tried - nor do I see how it would do the desired task. (Costas may want to explain it.)
                                  – Janis
                                  May 18 '15 at 18:17











                                • @ayrton_senna Up to you. It can be used in all variants.
                                  – Costas
                                  May 18 '15 at 18:18










                                • @Janis I have tested - it work
                                  – Costas
                                  May 18 '15 at 18:21










                                • @Costas; If I remove one of the lines in the data file, or add a data element AAAA - in both cases disturbing the order -, I still get a positive result, even though it shouldn't. - Are you sure you tested those cases?
                                  – Janis
                                  May 18 '15 at 18:33













                                up vote
                                0
                                down vote










                                up vote
                                0
                                down vote









                                If you'd like strict sequence it can be easy done by produce etalon sequence and compare it with file



                                [ $(comm --nocheck-order -3 
                                <(sed '/S/!d' file)
                                <(printf "%Xn"
                                $(seq $((16#$(sed '/./!d;q' file)))
                                $((16#$(tac file|sed '/./!d;q')))))) ] &&
                                echo 'Numbers are not in sequence' ||
                                echo 'All numbers are in sequence'


                                Just to check if all numbers are hexadecimal:



                                sed '/^[[:xdigit:] ]*$/!
                                s/.*/Numbers are not in sequence: &/
                                q

                                $! d
                                s/.*/All numbers are in sequence/' hexadecimal_number.list



                                • /^[[:xdigit:] ]*$/ check if in line there are hexadecimal symbols and space only (or nothing - empty line)


                                • ! reverse match, so execute commands in just if there some other symbols than described above


                                • s/.*/.../;q substitute all line by Numbers are not in sequence than quit script with printing line


                                • $! d if script didn't execute previous (pattern match) and if line is not last delete it it start with next line


                                • s/.*/.../ if script reach last line (so all lines match pattern) substitute last line by All numbers are in sequence an print it





                                share|improve this answer














                                If you'd like strict sequence it can be easy done by produce etalon sequence and compare it with file



                                [ $(comm --nocheck-order -3 
                                <(sed '/S/!d' file)
                                <(printf "%Xn"
                                $(seq $((16#$(sed '/./!d;q' file)))
                                $((16#$(tac file|sed '/./!d;q')))))) ] &&
                                echo 'Numbers are not in sequence' ||
                                echo 'All numbers are in sequence'


                                Just to check if all numbers are hexadecimal:



                                sed '/^[[:xdigit:] ]*$/!
                                s/.*/Numbers are not in sequence: &/
                                q

                                $! d
                                s/.*/All numbers are in sequence/' hexadecimal_number.list



                                • /^[[:xdigit:] ]*$/ check if in line there are hexadecimal symbols and space only (or nothing - empty line)


                                • ! reverse match, so execute commands in just if there some other symbols than described above


                                • s/.*/.../;q substitute all line by Numbers are not in sequence than quit script with printing line


                                • $! d if script didn't execute previous (pattern match) and if line is not last delete it it start with next line


                                • s/.*/.../ if script reach last line (so all lines match pattern) substitute last line by All numbers are in sequence an print it






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited May 18 '15 at 19:18

























                                answered May 18 '15 at 17:48









                                Costas

                                12.5k1029




                                12.5k1029











                                • should i put this in a script and run it or its a single line command? i am newbie, sorry for my ignorance.
                                  – ayrton_senna
                                  May 18 '15 at 18:02










                                • @ayrton_senna; Typically longer commands are put in a file (say yourscript) and executed by calling the file with a shell (e.g. bash yourscript). - But in this case I don't see that this code works - it certainly doesn't work when I tried - nor do I see how it would do the desired task. (Costas may want to explain it.)
                                  – Janis
                                  May 18 '15 at 18:17











                                • @ayrton_senna Up to you. It can be used in all variants.
                                  – Costas
                                  May 18 '15 at 18:18










                                • @Janis I have tested - it work
                                  – Costas
                                  May 18 '15 at 18:21










                                • @Costas; If I remove one of the lines in the data file, or add a data element AAAA - in both cases disturbing the order -, I still get a positive result, even though it shouldn't. - Are you sure you tested those cases?
                                  – Janis
                                  May 18 '15 at 18:33

















                                • should i put this in a script and run it or its a single line command? i am newbie, sorry for my ignorance.
                                  – ayrton_senna
                                  May 18 '15 at 18:02










                                • @ayrton_senna; Typically longer commands are put in a file (say yourscript) and executed by calling the file with a shell (e.g. bash yourscript). - But in this case I don't see that this code works - it certainly doesn't work when I tried - nor do I see how it would do the desired task. (Costas may want to explain it.)
                                  – Janis
                                  May 18 '15 at 18:17











                                • @ayrton_senna Up to you. It can be used in all variants.
                                  – Costas
                                  May 18 '15 at 18:18










                                • @Janis I have tested - it work
                                  – Costas
                                  May 18 '15 at 18:21










                                • @Costas; If I remove one of the lines in the data file, or add a data element AAAA - in both cases disturbing the order -, I still get a positive result, even though it shouldn't. - Are you sure you tested those cases?
                                  – Janis
                                  May 18 '15 at 18:33
















                                should i put this in a script and run it or its a single line command? i am newbie, sorry for my ignorance.
                                – ayrton_senna
                                May 18 '15 at 18:02




                                should i put this in a script and run it or its a single line command? i am newbie, sorry for my ignorance.
                                – ayrton_senna
                                May 18 '15 at 18:02












                                @ayrton_senna; Typically longer commands are put in a file (say yourscript) and executed by calling the file with a shell (e.g. bash yourscript). - But in this case I don't see that this code works - it certainly doesn't work when I tried - nor do I see how it would do the desired task. (Costas may want to explain it.)
                                – Janis
                                May 18 '15 at 18:17





                                @ayrton_senna; Typically longer commands are put in a file (say yourscript) and executed by calling the file with a shell (e.g. bash yourscript). - But in this case I don't see that this code works - it certainly doesn't work when I tried - nor do I see how it would do the desired task. (Costas may want to explain it.)
                                – Janis
                                May 18 '15 at 18:17













                                @ayrton_senna Up to you. It can be used in all variants.
                                – Costas
                                May 18 '15 at 18:18




                                @ayrton_senna Up to you. It can be used in all variants.
                                – Costas
                                May 18 '15 at 18:18












                                @Janis I have tested - it work
                                – Costas
                                May 18 '15 at 18:21




                                @Janis I have tested - it work
                                – Costas
                                May 18 '15 at 18:21












                                @Costas; If I remove one of the lines in the data file, or add a data element AAAA - in both cases disturbing the order -, I still get a positive result, even though it shouldn't. - Are you sure you tested those cases?
                                – Janis
                                May 18 '15 at 18:33





                                @Costas; If I remove one of the lines in the data file, or add a data element AAAA - in both cases disturbing the order -, I still get a positive result, even though it shouldn't. - Are you sure you tested those cases?
                                – Janis
                                May 18 '15 at 18:33











                                up vote
                                0
                                down vote













                                Given the input file hexlist.txt, this works in ksh and bash. It uses the numinterval util. If numinterval isn't available, uncomment the faker function:



                                # fake `numinterval` function
                                # numinterval() dc ;

                                n=; eval printf '%s\n' $(printf '$((16#%sn)) ' $(<hexlist.txt)) |
                                numinterval | grep -qw 1 || n=not ; echo All numbers are $n in sequence





                                share|improve this answer
























                                  up vote
                                  0
                                  down vote













                                  Given the input file hexlist.txt, this works in ksh and bash. It uses the numinterval util. If numinterval isn't available, uncomment the faker function:



                                  # fake `numinterval` function
                                  # numinterval() dc ;

                                  n=; eval printf '%s\n' $(printf '$((16#%sn)) ' $(<hexlist.txt)) |
                                  numinterval | grep -qw 1 || n=not ; echo All numbers are $n in sequence





                                  share|improve this answer






















                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    Given the input file hexlist.txt, this works in ksh and bash. It uses the numinterval util. If numinterval isn't available, uncomment the faker function:



                                    # fake `numinterval` function
                                    # numinterval() dc ;

                                    n=; eval printf '%s\n' $(printf '$((16#%sn)) ' $(<hexlist.txt)) |
                                    numinterval | grep -qw 1 || n=not ; echo All numbers are $n in sequence





                                    share|improve this answer












                                    Given the input file hexlist.txt, this works in ksh and bash. It uses the numinterval util. If numinterval isn't available, uncomment the faker function:



                                    # fake `numinterval` function
                                    # numinterval() dc ;

                                    n=; eval printf '%s\n' $(printf '$((16#%sn)) ' $(<hexlist.txt)) |
                                    numinterval | grep -qw 1 || n=not ; echo All numbers are $n in sequence






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Sep 27 at 20:22









                                    agc

                                    4,3221935




                                    4,3221935



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f204138%2fhexadecimal-number-sequence-checker-in-linux%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Popular posts from this blog

                                        How to check contact read email or not when send email to Individual?

                                        Bahrain

                                        Postfix configuration issue with fips on centos 7; mailgun relay