Cut in CLI doesn't work as expected - returns full lines every time












5















I'm running MacOS Mojave and I'm trying to get a list of owners of files in given path. I'm trying to do it like this but it doesn't work.



$ ls -l /usr | cut -f3
total 0
drwxr-xr-x 971 root wheel 31072 23 sty 20:05 bin
drwxr-xr-x 304 root wheel 9728 23 sty 20:05 lib
drwxr-xr-x 248 root wheel 7936 23 sty 20:05 libexec
drwxr-xr-x 16 root wheel 512 3 lis 10:50 local
drwxr-xr-x 239 root wheel 7648 23 sty 20:05 sbin
drwxr-xr-x 46 root wheel 1472 3 lis 10:41 share
drwxr-xr-x 5 root wheel 160 21 wrz 06:06 standalone


Specifying delimiters seems to work but not for the TAB character (which should be default).



$ ls -l /usr | cut -f3 -d' '

971
304
248

239


I'm using ZSH with Oh my zsh and iTerm 2 if it matters.










share|improve this question




















  • 2





    Can you add some details about what you intend to do with the list of owners? Depending on this there might be better solutions than parsing the output of ls.

    – nohillside
    Jan 27 at 14:22






  • 1





    Tab is, indeed, the default for cut, but the output of ls isn't tab-delimited; it just uses calculated numbers of space characters to produce tabular output.

    – chepner
    Jan 28 at 16:54


















5















I'm running MacOS Mojave and I'm trying to get a list of owners of files in given path. I'm trying to do it like this but it doesn't work.



$ ls -l /usr | cut -f3
total 0
drwxr-xr-x 971 root wheel 31072 23 sty 20:05 bin
drwxr-xr-x 304 root wheel 9728 23 sty 20:05 lib
drwxr-xr-x 248 root wheel 7936 23 sty 20:05 libexec
drwxr-xr-x 16 root wheel 512 3 lis 10:50 local
drwxr-xr-x 239 root wheel 7648 23 sty 20:05 sbin
drwxr-xr-x 46 root wheel 1472 3 lis 10:41 share
drwxr-xr-x 5 root wheel 160 21 wrz 06:06 standalone


Specifying delimiters seems to work but not for the TAB character (which should be default).



$ ls -l /usr | cut -f3 -d' '

971
304
248

239


I'm using ZSH with Oh my zsh and iTerm 2 if it matters.










share|improve this question




















  • 2





    Can you add some details about what you intend to do with the list of owners? Depending on this there might be better solutions than parsing the output of ls.

    – nohillside
    Jan 27 at 14:22






  • 1





    Tab is, indeed, the default for cut, but the output of ls isn't tab-delimited; it just uses calculated numbers of space characters to produce tabular output.

    – chepner
    Jan 28 at 16:54
















5












5








5


1






I'm running MacOS Mojave and I'm trying to get a list of owners of files in given path. I'm trying to do it like this but it doesn't work.



$ ls -l /usr | cut -f3
total 0
drwxr-xr-x 971 root wheel 31072 23 sty 20:05 bin
drwxr-xr-x 304 root wheel 9728 23 sty 20:05 lib
drwxr-xr-x 248 root wheel 7936 23 sty 20:05 libexec
drwxr-xr-x 16 root wheel 512 3 lis 10:50 local
drwxr-xr-x 239 root wheel 7648 23 sty 20:05 sbin
drwxr-xr-x 46 root wheel 1472 3 lis 10:41 share
drwxr-xr-x 5 root wheel 160 21 wrz 06:06 standalone


Specifying delimiters seems to work but not for the TAB character (which should be default).



$ ls -l /usr | cut -f3 -d' '

971
304
248

239


I'm using ZSH with Oh my zsh and iTerm 2 if it matters.










share|improve this question
















I'm running MacOS Mojave and I'm trying to get a list of owners of files in given path. I'm trying to do it like this but it doesn't work.



$ ls -l /usr | cut -f3
total 0
drwxr-xr-x 971 root wheel 31072 23 sty 20:05 bin
drwxr-xr-x 304 root wheel 9728 23 sty 20:05 lib
drwxr-xr-x 248 root wheel 7936 23 sty 20:05 libexec
drwxr-xr-x 16 root wheel 512 3 lis 10:50 local
drwxr-xr-x 239 root wheel 7648 23 sty 20:05 sbin
drwxr-xr-x 46 root wheel 1472 3 lis 10:41 share
drwxr-xr-x 5 root wheel 160 21 wrz 06:06 standalone


Specifying delimiters seems to work but not for the TAB character (which should be default).



$ ls -l /usr | cut -f3 -d' '

971
304
248

239


I'm using ZSH with Oh my zsh and iTerm 2 if it matters.







command-line zsh






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 27 at 14:23









nohillside

52.4k13112155




52.4k13112155










asked Jan 27 at 13:55









NajkiNajki

1284




1284








  • 2





    Can you add some details about what you intend to do with the list of owners? Depending on this there might be better solutions than parsing the output of ls.

    – nohillside
    Jan 27 at 14:22






  • 1





    Tab is, indeed, the default for cut, but the output of ls isn't tab-delimited; it just uses calculated numbers of space characters to produce tabular output.

    – chepner
    Jan 28 at 16:54
















  • 2





    Can you add some details about what you intend to do with the list of owners? Depending on this there might be better solutions than parsing the output of ls.

    – nohillside
    Jan 27 at 14:22






  • 1





    Tab is, indeed, the default for cut, but the output of ls isn't tab-delimited; it just uses calculated numbers of space characters to produce tabular output.

    – chepner
    Jan 28 at 16:54










2




2





Can you add some details about what you intend to do with the list of owners? Depending on this there might be better solutions than parsing the output of ls.

– nohillside
Jan 27 at 14:22





Can you add some details about what you intend to do with the list of owners? Depending on this there might be better solutions than parsing the output of ls.

– nohillside
Jan 27 at 14:22




1




1





Tab is, indeed, the default for cut, but the output of ls isn't tab-delimited; it just uses calculated numbers of space characters to produce tabular output.

– chepner
Jan 28 at 16:54







Tab is, indeed, the default for cut, but the output of ls isn't tab-delimited; it just uses calculated numbers of space characters to produce tabular output.

– chepner
Jan 28 at 16:54












2 Answers
2






active

oldest

votes


















17














You can squeeze the white spaces into a single white space in ls 's output then use cut.



ls -l /usr | tr -s ' ' | cut -d ' ' -f3


but avoid parsing ls output. Here's an alternate solution.



stat -f'%Su' /usr/*





share|improve this answer



















  • 6





    +1 for not parsing the output from ls — see unix.stackexchange.com/questions/128985, mywiki.wooledge.org/ParsingLs,&c.

    – gidds
    Jan 27 at 18:13











  • stat seems to be a good solution to my problem and lets me retrieve more info without all the hassle with extracting specific columns. Thanks!

    – Najki
    Jan 28 at 9:36






  • 1





    That second link should be just mywiki.wooledge.org/ParsingLs

    – Chris
    Jan 28 at 14:01



















11














ls doesn't use tabs, cut doesn't work with a variable number of delimeters between fields.



ls -l /usr | awk '{print $3}'


will work, or



ls -l /usr | awk 'NR > 1 {print $3}'


if you want to skip the first line (total 0 in your example).






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "118"
    };
    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%2fapple.stackexchange.com%2fquestions%2f349839%2fcut-in-cli-doesnt-work-as-expected-returns-full-lines-every-time%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









    17














    You can squeeze the white spaces into a single white space in ls 's output then use cut.



    ls -l /usr | tr -s ' ' | cut -d ' ' -f3


    but avoid parsing ls output. Here's an alternate solution.



    stat -f'%Su' /usr/*





    share|improve this answer



















    • 6





      +1 for not parsing the output from ls — see unix.stackexchange.com/questions/128985, mywiki.wooledge.org/ParsingLs,&c.

      – gidds
      Jan 27 at 18:13











    • stat seems to be a good solution to my problem and lets me retrieve more info without all the hassle with extracting specific columns. Thanks!

      – Najki
      Jan 28 at 9:36






    • 1





      That second link should be just mywiki.wooledge.org/ParsingLs

      – Chris
      Jan 28 at 14:01
















    17














    You can squeeze the white spaces into a single white space in ls 's output then use cut.



    ls -l /usr | tr -s ' ' | cut -d ' ' -f3


    but avoid parsing ls output. Here's an alternate solution.



    stat -f'%Su' /usr/*





    share|improve this answer



















    • 6





      +1 for not parsing the output from ls — see unix.stackexchange.com/questions/128985, mywiki.wooledge.org/ParsingLs,&c.

      – gidds
      Jan 27 at 18:13











    • stat seems to be a good solution to my problem and lets me retrieve more info without all the hassle with extracting specific columns. Thanks!

      – Najki
      Jan 28 at 9:36






    • 1





      That second link should be just mywiki.wooledge.org/ParsingLs

      – Chris
      Jan 28 at 14:01














    17












    17








    17







    You can squeeze the white spaces into a single white space in ls 's output then use cut.



    ls -l /usr | tr -s ' ' | cut -d ' ' -f3


    but avoid parsing ls output. Here's an alternate solution.



    stat -f'%Su' /usr/*





    share|improve this answer













    You can squeeze the white spaces into a single white space in ls 's output then use cut.



    ls -l /usr | tr -s ' ' | cut -d ' ' -f3


    but avoid parsing ls output. Here's an alternate solution.



    stat -f'%Su' /usr/*






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 27 at 16:39









    fd0fd0

    6,35011430




    6,35011430








    • 6





      +1 for not parsing the output from ls — see unix.stackexchange.com/questions/128985, mywiki.wooledge.org/ParsingLs,&c.

      – gidds
      Jan 27 at 18:13











    • stat seems to be a good solution to my problem and lets me retrieve more info without all the hassle with extracting specific columns. Thanks!

      – Najki
      Jan 28 at 9:36






    • 1





      That second link should be just mywiki.wooledge.org/ParsingLs

      – Chris
      Jan 28 at 14:01














    • 6





      +1 for not parsing the output from ls — see unix.stackexchange.com/questions/128985, mywiki.wooledge.org/ParsingLs,&c.

      – gidds
      Jan 27 at 18:13











    • stat seems to be a good solution to my problem and lets me retrieve more info without all the hassle with extracting specific columns. Thanks!

      – Najki
      Jan 28 at 9:36






    • 1





      That second link should be just mywiki.wooledge.org/ParsingLs

      – Chris
      Jan 28 at 14:01








    6




    6





    +1 for not parsing the output from ls — see unix.stackexchange.com/questions/128985, mywiki.wooledge.org/ParsingLs,&c.

    – gidds
    Jan 27 at 18:13





    +1 for not parsing the output from ls — see unix.stackexchange.com/questions/128985, mywiki.wooledge.org/ParsingLs,&c.

    – gidds
    Jan 27 at 18:13













    stat seems to be a good solution to my problem and lets me retrieve more info without all the hassle with extracting specific columns. Thanks!

    – Najki
    Jan 28 at 9:36





    stat seems to be a good solution to my problem and lets me retrieve more info without all the hassle with extracting specific columns. Thanks!

    – Najki
    Jan 28 at 9:36




    1




    1





    That second link should be just mywiki.wooledge.org/ParsingLs

    – Chris
    Jan 28 at 14:01





    That second link should be just mywiki.wooledge.org/ParsingLs

    – Chris
    Jan 28 at 14:01













    11














    ls doesn't use tabs, cut doesn't work with a variable number of delimeters between fields.



    ls -l /usr | awk '{print $3}'


    will work, or



    ls -l /usr | awk 'NR > 1 {print $3}'


    if you want to skip the first line (total 0 in your example).






    share|improve this answer




























      11














      ls doesn't use tabs, cut doesn't work with a variable number of delimeters between fields.



      ls -l /usr | awk '{print $3}'


      will work, or



      ls -l /usr | awk 'NR > 1 {print $3}'


      if you want to skip the first line (total 0 in your example).






      share|improve this answer


























        11












        11








        11







        ls doesn't use tabs, cut doesn't work with a variable number of delimeters between fields.



        ls -l /usr | awk '{print $3}'


        will work, or



        ls -l /usr | awk 'NR > 1 {print $3}'


        if you want to skip the first line (total 0 in your example).






        share|improve this answer













        ls doesn't use tabs, cut doesn't work with a variable number of delimeters between fields.



        ls -l /usr | awk '{print $3}'


        will work, or



        ls -l /usr | awk 'NR > 1 {print $3}'


        if you want to skip the first line (total 0 in your example).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 27 at 14:21









        nohillsidenohillside

        52.4k13112155




        52.4k13112155






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Ask Different!


            • 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%2fapple.stackexchange.com%2fquestions%2f349839%2fcut-in-cli-doesnt-work-as-expected-returns-full-lines-every-time%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

            Human spaceflight

            Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

            File:DeusFollowingSea.jpg