cut command fields

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











up vote
1
down vote

favorite












I noticed these two different fields behaviors using cut command:



bash:~$ var=`cat /proc/cpuinfo | grep 'model name' | uniq | cut -d ' ' -f 3,4,5,6,7,8 ` 
echo $var


outputs



Intel(R) Core(TM) i7-3632QM CPU @ 2.20GHz 


and:



bash:~$ echo `cat /proc/cpuinfo | grep 'model name' | uniq` | cut -d ' ' -f 3,4,5,6,7,8


outputs



: Intel(R) Core(TM) i7-3632QM CPU @ 


fields numbers are the same but different outputs. Why?










share|improve this question



























    up vote
    1
    down vote

    favorite












    I noticed these two different fields behaviors using cut command:



    bash:~$ var=`cat /proc/cpuinfo | grep 'model name' | uniq | cut -d ' ' -f 3,4,5,6,7,8 ` 
    echo $var


    outputs



    Intel(R) Core(TM) i7-3632QM CPU @ 2.20GHz 


    and:



    bash:~$ echo `cat /proc/cpuinfo | grep 'model name' | uniq` | cut -d ' ' -f 3,4,5,6,7,8


    outputs



    : Intel(R) Core(TM) i7-3632QM CPU @ 


    fields numbers are the same but different outputs. Why?










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I noticed these two different fields behaviors using cut command:



      bash:~$ var=`cat /proc/cpuinfo | grep 'model name' | uniq | cut -d ' ' -f 3,4,5,6,7,8 ` 
      echo $var


      outputs



      Intel(R) Core(TM) i7-3632QM CPU @ 2.20GHz 


      and:



      bash:~$ echo `cat /proc/cpuinfo | grep 'model name' | uniq` | cut -d ' ' -f 3,4,5,6,7,8


      outputs



      : Intel(R) Core(TM) i7-3632QM CPU @ 


      fields numbers are the same but different outputs. Why?










      share|improve this question















      I noticed these two different fields behaviors using cut command:



      bash:~$ var=`cat /proc/cpuinfo | grep 'model name' | uniq | cut -d ' ' -f 3,4,5,6,7,8 ` 
      echo $var


      outputs



      Intel(R) Core(TM) i7-3632QM CPU @ 2.20GHz 


      and:



      bash:~$ echo `cat /proc/cpuinfo | grep 'model name' | uniq` | cut -d ' ' -f 3,4,5,6,7,8


      outputs



      : Intel(R) Core(TM) i7-3632QM CPU @ 


      fields numbers are the same but different outputs. Why?







      grep cut command-substitution uniq






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 5 mins ago









      Jeff Schaller

      34.9k952115




      34.9k952115










      asked 1 hour ago









      pietro letti

      8117




      8117




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote













          It is because the unquoted `` backquote command substitution has removed an extra space between the model name and the : characters. Refer to the outputs without the grep to see the difference for yourself



          echo `cat /proc/cpuinfo | grep 'model name' | uniq`
          model name : Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz


          and with



          cat /proc/cpuinfo | grep 'model name' | uniq
          model name : Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz
          # ^^ - 2 spaces rather than one


          As a result the cut sees different fields from number 3 onwards in both the cases. This can be fixed if you avoid using backticks and use $(..) with a proper quoted substitution



          echo "$(cat /proc/cpuinfo | grep 'model name' | uniq)" | cut -d ' ' -f 3,4,5,6,7,8



          But that said, using cat/grep etc sequentially can be avoided and a single awk can be used in-place of it



          awk -F: '$1 ~ "model name" print $2 ' /proc/cpuinfo


          Or even more exact, if a single leading space in the above result is worrisome, remove it using sub



          awk -F: '$1 ~ "model name" sub(/^[[:space:]]/ ,"" , $2); print $2 ' /proc/cpuinfo


          Or if you have GNU variant of grep which has PCRE regex enabled, which you can use as



          grep -oP 'model name(s+):(s+)K(.+)' /proc/cpuinfo





          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: 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%2f479077%2fcut-command-fields%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote













            It is because the unquoted `` backquote command substitution has removed an extra space between the model name and the : characters. Refer to the outputs without the grep to see the difference for yourself



            echo `cat /proc/cpuinfo | grep 'model name' | uniq`
            model name : Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz


            and with



            cat /proc/cpuinfo | grep 'model name' | uniq
            model name : Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz
            # ^^ - 2 spaces rather than one


            As a result the cut sees different fields from number 3 onwards in both the cases. This can be fixed if you avoid using backticks and use $(..) with a proper quoted substitution



            echo "$(cat /proc/cpuinfo | grep 'model name' | uniq)" | cut -d ' ' -f 3,4,5,6,7,8



            But that said, using cat/grep etc sequentially can be avoided and a single awk can be used in-place of it



            awk -F: '$1 ~ "model name" print $2 ' /proc/cpuinfo


            Or even more exact, if a single leading space in the above result is worrisome, remove it using sub



            awk -F: '$1 ~ "model name" sub(/^[[:space:]]/ ,"" , $2); print $2 ' /proc/cpuinfo


            Or if you have GNU variant of grep which has PCRE regex enabled, which you can use as



            grep -oP 'model name(s+):(s+)K(.+)' /proc/cpuinfo





            share|improve this answer


























              up vote
              3
              down vote













              It is because the unquoted `` backquote command substitution has removed an extra space between the model name and the : characters. Refer to the outputs without the grep to see the difference for yourself



              echo `cat /proc/cpuinfo | grep 'model name' | uniq`
              model name : Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz


              and with



              cat /proc/cpuinfo | grep 'model name' | uniq
              model name : Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz
              # ^^ - 2 spaces rather than one


              As a result the cut sees different fields from number 3 onwards in both the cases. This can be fixed if you avoid using backticks and use $(..) with a proper quoted substitution



              echo "$(cat /proc/cpuinfo | grep 'model name' | uniq)" | cut -d ' ' -f 3,4,5,6,7,8



              But that said, using cat/grep etc sequentially can be avoided and a single awk can be used in-place of it



              awk -F: '$1 ~ "model name" print $2 ' /proc/cpuinfo


              Or even more exact, if a single leading space in the above result is worrisome, remove it using sub



              awk -F: '$1 ~ "model name" sub(/^[[:space:]]/ ,"" , $2); print $2 ' /proc/cpuinfo


              Or if you have GNU variant of grep which has PCRE regex enabled, which you can use as



              grep -oP 'model name(s+):(s+)K(.+)' /proc/cpuinfo





              share|improve this answer
























                up vote
                3
                down vote










                up vote
                3
                down vote









                It is because the unquoted `` backquote command substitution has removed an extra space between the model name and the : characters. Refer to the outputs without the grep to see the difference for yourself



                echo `cat /proc/cpuinfo | grep 'model name' | uniq`
                model name : Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz


                and with



                cat /proc/cpuinfo | grep 'model name' | uniq
                model name : Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz
                # ^^ - 2 spaces rather than one


                As a result the cut sees different fields from number 3 onwards in both the cases. This can be fixed if you avoid using backticks and use $(..) with a proper quoted substitution



                echo "$(cat /proc/cpuinfo | grep 'model name' | uniq)" | cut -d ' ' -f 3,4,5,6,7,8



                But that said, using cat/grep etc sequentially can be avoided and a single awk can be used in-place of it



                awk -F: '$1 ~ "model name" print $2 ' /proc/cpuinfo


                Or even more exact, if a single leading space in the above result is worrisome, remove it using sub



                awk -F: '$1 ~ "model name" sub(/^[[:space:]]/ ,"" , $2); print $2 ' /proc/cpuinfo


                Or if you have GNU variant of grep which has PCRE regex enabled, which you can use as



                grep -oP 'model name(s+):(s+)K(.+)' /proc/cpuinfo





                share|improve this answer














                It is because the unquoted `` backquote command substitution has removed an extra space between the model name and the : characters. Refer to the outputs without the grep to see the difference for yourself



                echo `cat /proc/cpuinfo | grep 'model name' | uniq`
                model name : Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz


                and with



                cat /proc/cpuinfo | grep 'model name' | uniq
                model name : Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz
                # ^^ - 2 spaces rather than one


                As a result the cut sees different fields from number 3 onwards in both the cases. This can be fixed if you avoid using backticks and use $(..) with a proper quoted substitution



                echo "$(cat /proc/cpuinfo | grep 'model name' | uniq)" | cut -d ' ' -f 3,4,5,6,7,8



                But that said, using cat/grep etc sequentially can be avoided and a single awk can be used in-place of it



                awk -F: '$1 ~ "model name" print $2 ' /proc/cpuinfo


                Or even more exact, if a single leading space in the above result is worrisome, remove it using sub



                awk -F: '$1 ~ "model name" sub(/^[[:space:]]/ ,"" , $2); print $2 ' /proc/cpuinfo


                Or if you have GNU variant of grep which has PCRE regex enabled, which you can use as



                grep -oP 'model name(s+):(s+)K(.+)' /proc/cpuinfo






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 37 mins ago

























                answered 53 mins ago









                Inian

                3,168822




                3,168822



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f479077%2fcut-command-fields%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    Confectionery