How to count the times a specific character appears in a file?












6














For example, we want to count all quote (") characters; we just worry if files have more quotes than it should.



For example:



cluster-env,"manage_dirs_on_root","true"
cluster-env,"one_dir_per_partition","false"
cluster-env,"override_uid","true"
cluster-env,"recovery_enabled","false"


expected results:



16










share|improve this question





























    6














    For example, we want to count all quote (") characters; we just worry if files have more quotes than it should.



    For example:



    cluster-env,"manage_dirs_on_root","true"
    cluster-env,"one_dir_per_partition","false"
    cluster-env,"override_uid","true"
    cluster-env,"recovery_enabled","false"


    expected results:



    16










    share|improve this question



























      6












      6








      6


      1





      For example, we want to count all quote (") characters; we just worry if files have more quotes than it should.



      For example:



      cluster-env,"manage_dirs_on_root","true"
      cluster-env,"one_dir_per_partition","false"
      cluster-env,"override_uid","true"
      cluster-env,"recovery_enabled","false"


      expected results:



      16










      share|improve this question















      For example, we want to count all quote (") characters; we just worry if files have more quotes than it should.



      For example:



      cluster-env,"manage_dirs_on_root","true"
      cluster-env,"one_dir_per_partition","false"
      cluster-env,"override_uid","true"
      cluster-env,"recovery_enabled","false"


      expected results:



      16







      text-processing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 22 '17 at 22:03









      Braiam

      23.1k1976138




      23.1k1976138










      asked Aug 22 '17 at 12:32









      yaelyael

      2,44112362




      2,44112362






















          7 Answers
          7






          active

          oldest

          votes


















          17














          You can combine tr (translate or delete characters) with wc (count words, lines, characters):



          tr -cd '"' < yourfile.cfg | wc -c


          (-delete all characters in the complement of ", and then count the characters.)






          share|improve this answer





























            11














            grep approach:



            grep -o '"' file | wc -l
            16




            • -o - output only matched substrings




            Or with single gawk:



            awk -v RS='' -v FPAT='"' '{print NF}' file
            16



            • RS='' - empty record separator (instead of newline)


            • FPAT='"' - pattern defining field value







            share|improve this answer























            • -o is a non-standard GNU extension to the standard grep utility. It's not mentioned in the POSIX documentation for grep.
              – Andrew Henle
              Aug 23 '17 at 11:09





















            4














            If two lines in the file has an odd number of double quotes, the total sum of double quotes will be even, and you will not detect unbalanced quotes (this is what I presume you'd like to actually do, but I might be wrong).



            This awk script reports any line in the input line that has an odd number of quotes:



            awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }'


            We set the field separator (FS) to " with -F'"' which means that if a line has an even number of fields it has odd quotes. NF is the number of fields in the recent record, and NR is the ordinal number of the current record ("the line number").



            Given the following input:



            $ cat file
            cluster-env,"manage_dirs_on_root","true"
            cluster-env,"one_dir_per_partition","false"
            cluster-env,override_uid","true"
            cluster-env,recovery_enabled","false"


            we get



            $ awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }' file
            Line 3 has odd quoting: cluster-env,override_uid","true"
            Line 4 has odd quoting: cluster-env,recovery_enabled","false"


            Something like



            $ grep -o '"' | wc -l


            would return "14" for this file.






            share|improve this answer































              2














              Another single gawk approach:



              awk -v RS=" 'END{print NR-1}'





              share|improve this answer





























                2














                Pure BASH:



                var="$(< file.txt)"
                tmp="${var//[^"]/}"
                echo ${#tmp}





                share|improve this answer























                • Is tmp an array? If yes, tmp is an array of what?
                  – Tim
                  Aug 23 '17 at 0:14












                • @Tim, no. tmp in this snippet is a normal shell variable. And I'm downvoting this answer because this counts the number of times a character appears in a variable (var) rather than in a file as specified in the question.
                  – Wildcard
                  Aug 23 '17 at 3:33



















                0














                try:



                grep -0 '"' File -c


                however, this will not work if two or more char in the same line. They will be counted as one char






                share|improve this answer

















                • 1




                  Welcome on U&L! This doesn't seem to actually answer the question, since it will count lines instead of characters. Writing effective answers is strongly encouraged on U&L - see Answering in the help center. You might want to improve this one.
                  – fra-san
                  Dec 1 '18 at 20:12



















                0














                grep -oF ' " ' file | wc -l



                -F stands for fixed string






                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',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: false,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  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%2funix.stackexchange.com%2fquestions%2f387656%2fhow-to-count-the-times-a-specific-character-appears-in-a-file%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  7 Answers
                  7






                  active

                  oldest

                  votes








                  7 Answers
                  7






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  17














                  You can combine tr (translate or delete characters) with wc (count words, lines, characters):



                  tr -cd '"' < yourfile.cfg | wc -c


                  (-delete all characters in the complement of ", and then count the characters.)






                  share|improve this answer


























                    17














                    You can combine tr (translate or delete characters) with wc (count words, lines, characters):



                    tr -cd '"' < yourfile.cfg | wc -c


                    (-delete all characters in the complement of ", and then count the characters.)






                    share|improve this answer
























                      17












                      17








                      17






                      You can combine tr (translate or delete characters) with wc (count words, lines, characters):



                      tr -cd '"' < yourfile.cfg | wc -c


                      (-delete all characters in the complement of ", and then count the characters.)






                      share|improve this answer












                      You can combine tr (translate or delete characters) with wc (count words, lines, characters):



                      tr -cd '"' < yourfile.cfg | wc -c


                      (-delete all characters in the complement of ", and then count the characters.)







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 22 '17 at 12:35









                      Ulrich SchwarzUlrich Schwarz

                      9,60512846




                      9,60512846

























                          11














                          grep approach:



                          grep -o '"' file | wc -l
                          16




                          • -o - output only matched substrings




                          Or with single gawk:



                          awk -v RS='' -v FPAT='"' '{print NF}' file
                          16



                          • RS='' - empty record separator (instead of newline)


                          • FPAT='"' - pattern defining field value







                          share|improve this answer























                          • -o is a non-standard GNU extension to the standard grep utility. It's not mentioned in the POSIX documentation for grep.
                            – Andrew Henle
                            Aug 23 '17 at 11:09


















                          11














                          grep approach:



                          grep -o '"' file | wc -l
                          16




                          • -o - output only matched substrings




                          Or with single gawk:



                          awk -v RS='' -v FPAT='"' '{print NF}' file
                          16



                          • RS='' - empty record separator (instead of newline)


                          • FPAT='"' - pattern defining field value







                          share|improve this answer























                          • -o is a non-standard GNU extension to the standard grep utility. It's not mentioned in the POSIX documentation for grep.
                            – Andrew Henle
                            Aug 23 '17 at 11:09
















                          11












                          11








                          11






                          grep approach:



                          grep -o '"' file | wc -l
                          16




                          • -o - output only matched substrings




                          Or with single gawk:



                          awk -v RS='' -v FPAT='"' '{print NF}' file
                          16



                          • RS='' - empty record separator (instead of newline)


                          • FPAT='"' - pattern defining field value







                          share|improve this answer














                          grep approach:



                          grep -o '"' file | wc -l
                          16




                          • -o - output only matched substrings




                          Or with single gawk:



                          awk -v RS='' -v FPAT='"' '{print NF}' file
                          16



                          • RS='' - empty record separator (instead of newline)


                          • FPAT='"' - pattern defining field value








                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Aug 22 '17 at 12:50

























                          answered Aug 22 '17 at 12:43









                          RomanPerekhrestRomanPerekhrest

                          22.9k12346




                          22.9k12346












                          • -o is a non-standard GNU extension to the standard grep utility. It's not mentioned in the POSIX documentation for grep.
                            – Andrew Henle
                            Aug 23 '17 at 11:09




















                          • -o is a non-standard GNU extension to the standard grep utility. It's not mentioned in the POSIX documentation for grep.
                            – Andrew Henle
                            Aug 23 '17 at 11:09


















                          -o is a non-standard GNU extension to the standard grep utility. It's not mentioned in the POSIX documentation for grep.
                          – Andrew Henle
                          Aug 23 '17 at 11:09






                          -o is a non-standard GNU extension to the standard grep utility. It's not mentioned in the POSIX documentation for grep.
                          – Andrew Henle
                          Aug 23 '17 at 11:09













                          4














                          If two lines in the file has an odd number of double quotes, the total sum of double quotes will be even, and you will not detect unbalanced quotes (this is what I presume you'd like to actually do, but I might be wrong).



                          This awk script reports any line in the input line that has an odd number of quotes:



                          awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }'


                          We set the field separator (FS) to " with -F'"' which means that if a line has an even number of fields it has odd quotes. NF is the number of fields in the recent record, and NR is the ordinal number of the current record ("the line number").



                          Given the following input:



                          $ cat file
                          cluster-env,"manage_dirs_on_root","true"
                          cluster-env,"one_dir_per_partition","false"
                          cluster-env,override_uid","true"
                          cluster-env,recovery_enabled","false"


                          we get



                          $ awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }' file
                          Line 3 has odd quoting: cluster-env,override_uid","true"
                          Line 4 has odd quoting: cluster-env,recovery_enabled","false"


                          Something like



                          $ grep -o '"' | wc -l


                          would return "14" for this file.






                          share|improve this answer




























                            4














                            If two lines in the file has an odd number of double quotes, the total sum of double quotes will be even, and you will not detect unbalanced quotes (this is what I presume you'd like to actually do, but I might be wrong).



                            This awk script reports any line in the input line that has an odd number of quotes:



                            awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }'


                            We set the field separator (FS) to " with -F'"' which means that if a line has an even number of fields it has odd quotes. NF is the number of fields in the recent record, and NR is the ordinal number of the current record ("the line number").



                            Given the following input:



                            $ cat file
                            cluster-env,"manage_dirs_on_root","true"
                            cluster-env,"one_dir_per_partition","false"
                            cluster-env,override_uid","true"
                            cluster-env,recovery_enabled","false"


                            we get



                            $ awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }' file
                            Line 3 has odd quoting: cluster-env,override_uid","true"
                            Line 4 has odd quoting: cluster-env,recovery_enabled","false"


                            Something like



                            $ grep -o '"' | wc -l


                            would return "14" for this file.






                            share|improve this answer


























                              4












                              4








                              4






                              If two lines in the file has an odd number of double quotes, the total sum of double quotes will be even, and you will not detect unbalanced quotes (this is what I presume you'd like to actually do, but I might be wrong).



                              This awk script reports any line in the input line that has an odd number of quotes:



                              awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }'


                              We set the field separator (FS) to " with -F'"' which means that if a line has an even number of fields it has odd quotes. NF is the number of fields in the recent record, and NR is the ordinal number of the current record ("the line number").



                              Given the following input:



                              $ cat file
                              cluster-env,"manage_dirs_on_root","true"
                              cluster-env,"one_dir_per_partition","false"
                              cluster-env,override_uid","true"
                              cluster-env,recovery_enabled","false"


                              we get



                              $ awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }' file
                              Line 3 has odd quoting: cluster-env,override_uid","true"
                              Line 4 has odd quoting: cluster-env,recovery_enabled","false"


                              Something like



                              $ grep -o '"' | wc -l


                              would return "14" for this file.






                              share|improve this answer














                              If two lines in the file has an odd number of double quotes, the total sum of double quotes will be even, and you will not detect unbalanced quotes (this is what I presume you'd like to actually do, but I might be wrong).



                              This awk script reports any line in the input line that has an odd number of quotes:



                              awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }'


                              We set the field separator (FS) to " with -F'"' which means that if a line has an even number of fields it has odd quotes. NF is the number of fields in the recent record, and NR is the ordinal number of the current record ("the line number").



                              Given the following input:



                              $ cat file
                              cluster-env,"manage_dirs_on_root","true"
                              cluster-env,"one_dir_per_partition","false"
                              cluster-env,override_uid","true"
                              cluster-env,recovery_enabled","false"


                              we get



                              $ awk -F'"' 'NF % 2 == 0 { printf("Line %d has odd quoting: %sn", NR, $0) }' file
                              Line 3 has odd quoting: cluster-env,override_uid","true"
                              Line 4 has odd quoting: cluster-env,recovery_enabled","false"


                              Something like



                              $ grep -o '"' | wc -l


                              would return "14" for this file.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Aug 22 '17 at 16:21

























                              answered Aug 22 '17 at 12:49









                              KusalanandaKusalananda

                              122k16230375




                              122k16230375























                                  2














                                  Another single gawk approach:



                                  awk -v RS=" 'END{print NR-1}'





                                  share|improve this answer


























                                    2














                                    Another single gawk approach:



                                    awk -v RS=" 'END{print NR-1}'





                                    share|improve this answer
























                                      2












                                      2








                                      2






                                      Another single gawk approach:



                                      awk -v RS=" 'END{print NR-1}'





                                      share|improve this answer












                                      Another single gawk approach:



                                      awk -v RS=" 'END{print NR-1}'






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Aug 22 '17 at 19:06









                                      αғsнιηαғsнιη

                                      16.6k102865




                                      16.6k102865























                                          2














                                          Pure BASH:



                                          var="$(< file.txt)"
                                          tmp="${var//[^"]/}"
                                          echo ${#tmp}





                                          share|improve this answer























                                          • Is tmp an array? If yes, tmp is an array of what?
                                            – Tim
                                            Aug 23 '17 at 0:14












                                          • @Tim, no. tmp in this snippet is a normal shell variable. And I'm downvoting this answer because this counts the number of times a character appears in a variable (var) rather than in a file as specified in the question.
                                            – Wildcard
                                            Aug 23 '17 at 3:33
















                                          2














                                          Pure BASH:



                                          var="$(< file.txt)"
                                          tmp="${var//[^"]/}"
                                          echo ${#tmp}





                                          share|improve this answer























                                          • Is tmp an array? If yes, tmp is an array of what?
                                            – Tim
                                            Aug 23 '17 at 0:14












                                          • @Tim, no. tmp in this snippet is a normal shell variable. And I'm downvoting this answer because this counts the number of times a character appears in a variable (var) rather than in a file as specified in the question.
                                            – Wildcard
                                            Aug 23 '17 at 3:33














                                          2












                                          2








                                          2






                                          Pure BASH:



                                          var="$(< file.txt)"
                                          tmp="${var//[^"]/}"
                                          echo ${#tmp}





                                          share|improve this answer














                                          Pure BASH:



                                          var="$(< file.txt)"
                                          tmp="${var//[^"]/}"
                                          echo ${#tmp}






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Aug 23 '17 at 7:15

























                                          answered Aug 22 '17 at 18:49









                                          ThunderbeefThunderbeef

                                          1606




                                          1606












                                          • Is tmp an array? If yes, tmp is an array of what?
                                            – Tim
                                            Aug 23 '17 at 0:14












                                          • @Tim, no. tmp in this snippet is a normal shell variable. And I'm downvoting this answer because this counts the number of times a character appears in a variable (var) rather than in a file as specified in the question.
                                            – Wildcard
                                            Aug 23 '17 at 3:33


















                                          • Is tmp an array? If yes, tmp is an array of what?
                                            – Tim
                                            Aug 23 '17 at 0:14












                                          • @Tim, no. tmp in this snippet is a normal shell variable. And I'm downvoting this answer because this counts the number of times a character appears in a variable (var) rather than in a file as specified in the question.
                                            – Wildcard
                                            Aug 23 '17 at 3:33
















                                          Is tmp an array? If yes, tmp is an array of what?
                                          – Tim
                                          Aug 23 '17 at 0:14






                                          Is tmp an array? If yes, tmp is an array of what?
                                          – Tim
                                          Aug 23 '17 at 0:14














                                          @Tim, no. tmp in this snippet is a normal shell variable. And I'm downvoting this answer because this counts the number of times a character appears in a variable (var) rather than in a file as specified in the question.
                                          – Wildcard
                                          Aug 23 '17 at 3:33




                                          @Tim, no. tmp in this snippet is a normal shell variable. And I'm downvoting this answer because this counts the number of times a character appears in a variable (var) rather than in a file as specified in the question.
                                          – Wildcard
                                          Aug 23 '17 at 3:33











                                          0














                                          try:



                                          grep -0 '"' File -c


                                          however, this will not work if two or more char in the same line. They will be counted as one char






                                          share|improve this answer

















                                          • 1




                                            Welcome on U&L! This doesn't seem to actually answer the question, since it will count lines instead of characters. Writing effective answers is strongly encouraged on U&L - see Answering in the help center. You might want to improve this one.
                                            – fra-san
                                            Dec 1 '18 at 20:12
















                                          0














                                          try:



                                          grep -0 '"' File -c


                                          however, this will not work if two or more char in the same line. They will be counted as one char






                                          share|improve this answer

















                                          • 1




                                            Welcome on U&L! This doesn't seem to actually answer the question, since it will count lines instead of characters. Writing effective answers is strongly encouraged on U&L - see Answering in the help center. You might want to improve this one.
                                            – fra-san
                                            Dec 1 '18 at 20:12














                                          0












                                          0








                                          0






                                          try:



                                          grep -0 '"' File -c


                                          however, this will not work if two or more char in the same line. They will be counted as one char






                                          share|improve this answer












                                          try:



                                          grep -0 '"' File -c


                                          however, this will not work if two or more char in the same line. They will be counted as one char







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Dec 1 '18 at 19:41









                                          Abdulkarim MalkadiAbdulkarim Malkadi

                                          1




                                          1








                                          • 1




                                            Welcome on U&L! This doesn't seem to actually answer the question, since it will count lines instead of characters. Writing effective answers is strongly encouraged on U&L - see Answering in the help center. You might want to improve this one.
                                            – fra-san
                                            Dec 1 '18 at 20:12














                                          • 1




                                            Welcome on U&L! This doesn't seem to actually answer the question, since it will count lines instead of characters. Writing effective answers is strongly encouraged on U&L - see Answering in the help center. You might want to improve this one.
                                            – fra-san
                                            Dec 1 '18 at 20:12








                                          1




                                          1




                                          Welcome on U&L! This doesn't seem to actually answer the question, since it will count lines instead of characters. Writing effective answers is strongly encouraged on U&L - see Answering in the help center. You might want to improve this one.
                                          – fra-san
                                          Dec 1 '18 at 20:12




                                          Welcome on U&L! This doesn't seem to actually answer the question, since it will count lines instead of characters. Writing effective answers is strongly encouraged on U&L - see Answering in the help center. You might want to improve this one.
                                          – fra-san
                                          Dec 1 '18 at 20:12











                                          0














                                          grep -oF ' " ' file | wc -l



                                          -F stands for fixed string






                                          share|improve this answer


























                                            0














                                            grep -oF ' " ' file | wc -l



                                            -F stands for fixed string






                                            share|improve this answer
























                                              0












                                              0








                                              0






                                              grep -oF ' " ' file | wc -l



                                              -F stands for fixed string






                                              share|improve this answer












                                              grep -oF ' " ' file | wc -l



                                              -F stands for fixed string







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered 13 mins ago









                                              shinekshinek

                                              87116




                                              87116






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                                  • 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.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • 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%2funix.stackexchange.com%2fquestions%2f387656%2fhow-to-count-the-times-a-specific-character-appears-in-a-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

                                                  CARDNET

                                                  Boot-repair Failure: Unable to locate package grub-common:i386

                                                  濃尾地震