determining the number of digits in text file












0















I have a data file as follows:



 15 01 01 00 00  0.0000000  0 22E19R13R12G15G26G24G21E20R19G13G18G29
15 01 01 01 00 30.0000000 0 22E19R13R12G15G26G24G21E20R19G13G18G29
15 01 01 02 01 0.0000000 0 22E19R13R12G15G26G24G21E20R19G13G18G29
15 01 01 03 01 30.0000000 0 22E19R13R12G15G26G24G21E20R19G13G18G29


I need to count the digit number of the value in the first line and fourth column then I need to determine this digit number as a variable as follows:



first line and fourth column: 00



digit_number=2









share|improve this question























  • Would be stack overflow the perfect place for this question?

    – TDK
    Jan 22 at 13:44











  • You can probably do this with sed and awk sed 's/[^0-9]//g' dat | awk '{ print length }' I'm neither an expert in sed nor awk so I expect someone to tell me this is very wrong :-) (also this only counts up all the numbers rather than just the first line and fourth column, again not an awk expert)

    – j-money
    Jan 22 at 13:47
















0















I have a data file as follows:



 15 01 01 00 00  0.0000000  0 22E19R13R12G15G26G24G21E20R19G13G18G29
15 01 01 01 00 30.0000000 0 22E19R13R12G15G26G24G21E20R19G13G18G29
15 01 01 02 01 0.0000000 0 22E19R13R12G15G26G24G21E20R19G13G18G29
15 01 01 03 01 30.0000000 0 22E19R13R12G15G26G24G21E20R19G13G18G29


I need to count the digit number of the value in the first line and fourth column then I need to determine this digit number as a variable as follows:



first line and fourth column: 00



digit_number=2









share|improve this question























  • Would be stack overflow the perfect place for this question?

    – TDK
    Jan 22 at 13:44











  • You can probably do this with sed and awk sed 's/[^0-9]//g' dat | awk '{ print length }' I'm neither an expert in sed nor awk so I expect someone to tell me this is very wrong :-) (also this only counts up all the numbers rather than just the first line and fourth column, again not an awk expert)

    – j-money
    Jan 22 at 13:47














0












0








0








I have a data file as follows:



 15 01 01 00 00  0.0000000  0 22E19R13R12G15G26G24G21E20R19G13G18G29
15 01 01 01 00 30.0000000 0 22E19R13R12G15G26G24G21E20R19G13G18G29
15 01 01 02 01 0.0000000 0 22E19R13R12G15G26G24G21E20R19G13G18G29
15 01 01 03 01 30.0000000 0 22E19R13R12G15G26G24G21E20R19G13G18G29


I need to count the digit number of the value in the first line and fourth column then I need to determine this digit number as a variable as follows:



first line and fourth column: 00



digit_number=2









share|improve this question














I have a data file as follows:



 15 01 01 00 00  0.0000000  0 22E19R13R12G15G26G24G21E20R19G13G18G29
15 01 01 01 00 30.0000000 0 22E19R13R12G15G26G24G21E20R19G13G18G29
15 01 01 02 01 0.0000000 0 22E19R13R12G15G26G24G21E20R19G13G18G29
15 01 01 03 01 30.0000000 0 22E19R13R12G15G26G24G21E20R19G13G18G29


I need to count the digit number of the value in the first line and fourth column then I need to determine this digit number as a variable as follows:



first line and fourth column: 00



digit_number=2






scripts






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 22 at 13:42









deepblue_86deepblue_86

5871025




5871025













  • Would be stack overflow the perfect place for this question?

    – TDK
    Jan 22 at 13:44











  • You can probably do this with sed and awk sed 's/[^0-9]//g' dat | awk '{ print length }' I'm neither an expert in sed nor awk so I expect someone to tell me this is very wrong :-) (also this only counts up all the numbers rather than just the first line and fourth column, again not an awk expert)

    – j-money
    Jan 22 at 13:47



















  • Would be stack overflow the perfect place for this question?

    – TDK
    Jan 22 at 13:44











  • You can probably do this with sed and awk sed 's/[^0-9]//g' dat | awk '{ print length }' I'm neither an expert in sed nor awk so I expect someone to tell me this is very wrong :-) (also this only counts up all the numbers rather than just the first line and fourth column, again not an awk expert)

    – j-money
    Jan 22 at 13:47

















Would be stack overflow the perfect place for this question?

– TDK
Jan 22 at 13:44





Would be stack overflow the perfect place for this question?

– TDK
Jan 22 at 13:44













You can probably do this with sed and awk sed 's/[^0-9]//g' dat | awk '{ print length }' I'm neither an expert in sed nor awk so I expect someone to tell me this is very wrong :-) (also this only counts up all the numbers rather than just the first line and fourth column, again not an awk expert)

– j-money
Jan 22 at 13:47





You can probably do this with sed and awk sed 's/[^0-9]//g' dat | awk '{ print length }' I'm neither an expert in sed nor awk so I expect someone to tell me this is very wrong :-) (also this only counts up all the numbers rather than just the first line and fourth column, again not an awk expert)

– j-money
Jan 22 at 13:47










2 Answers
2






active

oldest

votes


















3














If you don't need to actually check that the value is a number, you can simply check the length of the 4th field from the 1st record



awk 'FNR == 1 {print length($4); exit}' file


To assign to a shell variable:



digit_number=$(awk 'FNR == 1 {print length($4); exit}' file)




If you need to count how many digits are in the field, then you could modify the awk command to



awk 'FNR == 1 {print gsub(/[0-9]/,"",$4); exit}' file


Note that gsub will return a count of decimal digits [0-9] ignoring any non-digit characters in the field.






share|improve this answer































    1














    digit_number=$(head -1 {the file} | cut -d ' ' -f 4 | tr -d 'n' | wc -c)


    Where:





    • head -1 {the file} print the first line of the file


    • cut -d ' ' -f 4 takes the 4th space-delimited field


    • tr -d 'n' removes the trailing lf


    • wc -c counts the bytes in the result






    share|improve this answer

























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "89"
      };
      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1111965%2fdetermining-the-number-of-digits-in-text-file%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      If you don't need to actually check that the value is a number, you can simply check the length of the 4th field from the 1st record



      awk 'FNR == 1 {print length($4); exit}' file


      To assign to a shell variable:



      digit_number=$(awk 'FNR == 1 {print length($4); exit}' file)




      If you need to count how many digits are in the field, then you could modify the awk command to



      awk 'FNR == 1 {print gsub(/[0-9]/,"",$4); exit}' file


      Note that gsub will return a count of decimal digits [0-9] ignoring any non-digit characters in the field.






      share|improve this answer




























        3














        If you don't need to actually check that the value is a number, you can simply check the length of the 4th field from the 1st record



        awk 'FNR == 1 {print length($4); exit}' file


        To assign to a shell variable:



        digit_number=$(awk 'FNR == 1 {print length($4); exit}' file)




        If you need to count how many digits are in the field, then you could modify the awk command to



        awk 'FNR == 1 {print gsub(/[0-9]/,"",$4); exit}' file


        Note that gsub will return a count of decimal digits [0-9] ignoring any non-digit characters in the field.






        share|improve this answer


























          3












          3








          3







          If you don't need to actually check that the value is a number, you can simply check the length of the 4th field from the 1st record



          awk 'FNR == 1 {print length($4); exit}' file


          To assign to a shell variable:



          digit_number=$(awk 'FNR == 1 {print length($4); exit}' file)




          If you need to count how many digits are in the field, then you could modify the awk command to



          awk 'FNR == 1 {print gsub(/[0-9]/,"",$4); exit}' file


          Note that gsub will return a count of decimal digits [0-9] ignoring any non-digit characters in the field.






          share|improve this answer













          If you don't need to actually check that the value is a number, you can simply check the length of the 4th field from the 1st record



          awk 'FNR == 1 {print length($4); exit}' file


          To assign to a shell variable:



          digit_number=$(awk 'FNR == 1 {print length($4); exit}' file)




          If you need to count how many digits are in the field, then you could modify the awk command to



          awk 'FNR == 1 {print gsub(/[0-9]/,"",$4); exit}' file


          Note that gsub will return a count of decimal digits [0-9] ignoring any non-digit characters in the field.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 22 at 13:49









          steeldriversteeldriver

          68.7k11113184




          68.7k11113184

























              1














              digit_number=$(head -1 {the file} | cut -d ' ' -f 4 | tr -d 'n' | wc -c)


              Where:





              • head -1 {the file} print the first line of the file


              • cut -d ' ' -f 4 takes the 4th space-delimited field


              • tr -d 'n' removes the trailing lf


              • wc -c counts the bytes in the result






              share|improve this answer






























                1














                digit_number=$(head -1 {the file} | cut -d ' ' -f 4 | tr -d 'n' | wc -c)


                Where:





                • head -1 {the file} print the first line of the file


                • cut -d ' ' -f 4 takes the 4th space-delimited field


                • tr -d 'n' removes the trailing lf


                • wc -c counts the bytes in the result






                share|improve this answer




























                  1












                  1








                  1







                  digit_number=$(head -1 {the file} | cut -d ' ' -f 4 | tr -d 'n' | wc -c)


                  Where:





                  • head -1 {the file} print the first line of the file


                  • cut -d ' ' -f 4 takes the 4th space-delimited field


                  • tr -d 'n' removes the trailing lf


                  • wc -c counts the bytes in the result






                  share|improve this answer















                  digit_number=$(head -1 {the file} | cut -d ' ' -f 4 | tr -d 'n' | wc -c)


                  Where:





                  • head -1 {the file} print the first line of the file


                  • cut -d ' ' -f 4 takes the 4th space-delimited field


                  • tr -d 'n' removes the trailing lf


                  • wc -c counts the bytes in the result







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 22 at 13:53









                  pa4080

                  14.4k52670




                  14.4k52670










                  answered Jan 22 at 13:51









                  xenoidxenoid

                  1,8131416




                  1,8131416






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Ask Ubuntu!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1111965%2fdetermining-the-number-of-digits-in-text-file%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

                      How to change which sound is reproduced for terminal bell?

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?