How to count words in a line












3















I have a text file called "shoplist.txt" which one have:



drinks water cola fanta
fruit banana orange


And I want to get how many items per line I have. I'm able to extract drinks and fruit with function "cut" but how can I count how many words I have in each line?



My actually code is:



fileLine=`cat file.txt`
#Here I get each line saving it to fileLine
for line in $fileLine; do
echo
((aux++))
done


But this code dosen't work because it save to %fileLine each work (drinks, then water,then cola,...)



How can I get the first line and then count the words on that line?










share|improve this question





























    3















    I have a text file called "shoplist.txt" which one have:



    drinks water cola fanta
    fruit banana orange


    And I want to get how many items per line I have. I'm able to extract drinks and fruit with function "cut" but how can I count how many words I have in each line?



    My actually code is:



    fileLine=`cat file.txt`
    #Here I get each line saving it to fileLine
    for line in $fileLine; do
    echo
    ((aux++))
    done


    But this code dosen't work because it save to %fileLine each work (drinks, then water,then cola,...)



    How can I get the first line and then count the words on that line?










    share|improve this question



























      3












      3








      3








      I have a text file called "shoplist.txt" which one have:



      drinks water cola fanta
      fruit banana orange


      And I want to get how many items per line I have. I'm able to extract drinks and fruit with function "cut" but how can I count how many words I have in each line?



      My actually code is:



      fileLine=`cat file.txt`
      #Here I get each line saving it to fileLine
      for line in $fileLine; do
      echo
      ((aux++))
      done


      But this code dosen't work because it save to %fileLine each work (drinks, then water,then cola,...)



      How can I get the first line and then count the words on that line?










      share|improve this question
















      I have a text file called "shoplist.txt" which one have:



      drinks water cola fanta
      fruit banana orange


      And I want to get how many items per line I have. I'm able to extract drinks and fruit with function "cut" but how can I count how many words I have in each line?



      My actually code is:



      fileLine=`cat file.txt`
      #Here I get each line saving it to fileLine
      for line in $fileLine; do
      echo
      ((aux++))
      done


      But this code dosen't work because it save to %fileLine each work (drinks, then water,then cola,...)



      How can I get the first line and then count the words on that line?







      bash shell-script






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 10 hours ago









      Rui F Ribeiro

      41k1479137




      41k1479137










      asked 10 hours ago









      Multi17Multi17

      182




      182






















          2 Answers
          2






          active

          oldest

          votes


















          13














          If you can use awk, NF is the number of fields in the current line (by default, a field is a word delimited by any amount of whitespace).



          Use



          awk '{ print NF, $0 }' inputfile


          With your sample input, this will print



          4 drinks water cola fanta
          3 fruit banana orange





          share|improve this answer


























          • The more cryptic awk '$0=NF" "$0' inputfile is a shorter solution.

            – Isaac
            1 hour ago



















          2














          In Bash and wc:



          IFS=$'n'
          while read line; do
          wc -w <<< "$line"
          done < file.txt


          wc counts lines, words, bytes in files. With a shell loop you can make it count words in a line.






          share|improve this answer





















          • 1





            @Stobor thanks, fixed.

            – sborsky
            7 hours ago











          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%2f504326%2fhow-to-count-words-in-a-line%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









          13














          If you can use awk, NF is the number of fields in the current line (by default, a field is a word delimited by any amount of whitespace).



          Use



          awk '{ print NF, $0 }' inputfile


          With your sample input, this will print



          4 drinks water cola fanta
          3 fruit banana orange





          share|improve this answer


























          • The more cryptic awk '$0=NF" "$0' inputfile is a shorter solution.

            – Isaac
            1 hour ago
















          13














          If you can use awk, NF is the number of fields in the current line (by default, a field is a word delimited by any amount of whitespace).



          Use



          awk '{ print NF, $0 }' inputfile


          With your sample input, this will print



          4 drinks water cola fanta
          3 fruit banana orange





          share|improve this answer


























          • The more cryptic awk '$0=NF" "$0' inputfile is a shorter solution.

            – Isaac
            1 hour ago














          13












          13








          13







          If you can use awk, NF is the number of fields in the current line (by default, a field is a word delimited by any amount of whitespace).



          Use



          awk '{ print NF, $0 }' inputfile


          With your sample input, this will print



          4 drinks water cola fanta
          3 fruit banana orange





          share|improve this answer















          If you can use awk, NF is the number of fields in the current line (by default, a field is a word delimited by any amount of whitespace).



          Use



          awk '{ print NF, $0 }' inputfile


          With your sample input, this will print



          4 drinks water cola fanta
          3 fruit banana orange






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 7 hours ago









          Kusalananda

          133k17254417




          133k17254417










          answered 10 hours ago









          BodoBodo

          1,993314




          1,993314













          • The more cryptic awk '$0=NF" "$0' inputfile is a shorter solution.

            – Isaac
            1 hour ago



















          • The more cryptic awk '$0=NF" "$0' inputfile is a shorter solution.

            – Isaac
            1 hour ago

















          The more cryptic awk '$0=NF" "$0' inputfile is a shorter solution.

          – Isaac
          1 hour ago





          The more cryptic awk '$0=NF" "$0' inputfile is a shorter solution.

          – Isaac
          1 hour ago













          2














          In Bash and wc:



          IFS=$'n'
          while read line; do
          wc -w <<< "$line"
          done < file.txt


          wc counts lines, words, bytes in files. With a shell loop you can make it count words in a line.






          share|improve this answer





















          • 1





            @Stobor thanks, fixed.

            – sborsky
            7 hours ago
















          2














          In Bash and wc:



          IFS=$'n'
          while read line; do
          wc -w <<< "$line"
          done < file.txt


          wc counts lines, words, bytes in files. With a shell loop you can make it count words in a line.






          share|improve this answer





















          • 1





            @Stobor thanks, fixed.

            – sborsky
            7 hours ago














          2












          2








          2







          In Bash and wc:



          IFS=$'n'
          while read line; do
          wc -w <<< "$line"
          done < file.txt


          wc counts lines, words, bytes in files. With a shell loop you can make it count words in a line.






          share|improve this answer















          In Bash and wc:



          IFS=$'n'
          while read line; do
          wc -w <<< "$line"
          done < file.txt


          wc counts lines, words, bytes in files. With a shell loop you can make it count words in a line.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 7 hours ago

























          answered 9 hours ago









          sborskysborsky

          789511




          789511








          • 1





            @Stobor thanks, fixed.

            – sborsky
            7 hours ago














          • 1





            @Stobor thanks, fixed.

            – sborsky
            7 hours ago








          1




          1





          @Stobor thanks, fixed.

          – sborsky
          7 hours ago





          @Stobor thanks, fixed.

          – sborsky
          7 hours ago


















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504326%2fhow-to-count-words-in-a-line%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

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy