Using pipes to list the first two and last two users on the system?











up vote
2
down vote

favorite












I have to use the who command to display who's online, then use pipes to display the first and last 2 users online. the only thing I know how to do is something like:



who | head -5 | tail -2 .



That doesn't work, though.










share|improve this question
























  • you can combine multiple commands inside () or {} separated by ;... see Command-Grouping
    – Sundeep
    Feb 23 '17 at 3:02










  • First and last users in terms of what? Just from the output?
    – DarkHeart
    Feb 23 '17 at 4:48










  • Similar: Command to display first few and last few lines of a file
    – Stéphane Chazelas
    Feb 23 '17 at 16:19















up vote
2
down vote

favorite












I have to use the who command to display who's online, then use pipes to display the first and last 2 users online. the only thing I know how to do is something like:



who | head -5 | tail -2 .



That doesn't work, though.










share|improve this question
























  • you can combine multiple commands inside () or {} separated by ;... see Command-Grouping
    – Sundeep
    Feb 23 '17 at 3:02










  • First and last users in terms of what? Just from the output?
    – DarkHeart
    Feb 23 '17 at 4:48










  • Similar: Command to display first few and last few lines of a file
    – Stéphane Chazelas
    Feb 23 '17 at 16:19













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have to use the who command to display who's online, then use pipes to display the first and last 2 users online. the only thing I know how to do is something like:



who | head -5 | tail -2 .



That doesn't work, though.










share|improve this question















I have to use the who command to display who's online, then use pipes to display the first and last 2 users online. the only thing I know how to do is something like:



who | head -5 | tail -2 .



That doesn't work, though.







pipe tail head who






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 at 20:19









Rui F Ribeiro

38.3k1475126




38.3k1475126










asked Feb 23 '17 at 2:41









user217538

111




111












  • you can combine multiple commands inside () or {} separated by ;... see Command-Grouping
    – Sundeep
    Feb 23 '17 at 3:02










  • First and last users in terms of what? Just from the output?
    – DarkHeart
    Feb 23 '17 at 4:48










  • Similar: Command to display first few and last few lines of a file
    – Stéphane Chazelas
    Feb 23 '17 at 16:19


















  • you can combine multiple commands inside () or {} separated by ;... see Command-Grouping
    – Sundeep
    Feb 23 '17 at 3:02










  • First and last users in terms of what? Just from the output?
    – DarkHeart
    Feb 23 '17 at 4:48










  • Similar: Command to display first few and last few lines of a file
    – Stéphane Chazelas
    Feb 23 '17 at 16:19
















you can combine multiple commands inside () or {} separated by ;... see Command-Grouping
– Sundeep
Feb 23 '17 at 3:02




you can combine multiple commands inside () or {} separated by ;... see Command-Grouping
– Sundeep
Feb 23 '17 at 3:02












First and last users in terms of what? Just from the output?
– DarkHeart
Feb 23 '17 at 4:48




First and last users in terms of what? Just from the output?
– DarkHeart
Feb 23 '17 at 4:48












Similar: Command to display first few and last few lines of a file
– Stéphane Chazelas
Feb 23 '17 at 16:19




Similar: Command to display first few and last few lines of a file
– Stéphane Chazelas
Feb 23 '17 at 16:19










3 Answers
3






active

oldest

votes

















up vote
4
down vote













Directly:



who | head -2
who | tail -2





share|improve this answer




























    up vote
    0
    down vote













    You can use tee and a redirection to stderr



    who | tee >(head -n2 1>&2 ) | tail -n2


    Where tee allows you to copy stdin to both a file and stdout. Here we actually replace the file by the head command and redirect its output from stdout to stderr (via 1>&2) as all of stout is piped to the tail command and processed, so we need to circumvent tailing the head result, too. stderr itself is however still printed on the terminal.





    Updates following discussion in comments



    As pointed out by Stéphane, head might send a SIGPIPE signal kill the pipe prematurely so that tail might not see the actual end of the input stream.



    To prevent this, one could ignore the SIGPIPE signal as described here, by using a trap with an empty command. Tested in bash only .....



    who | { trap "" PIPE ; tee >( head -n 2 3>&1 >&2  ) ; } | tail -n 2


    Additionally mentioned in the comments: non-bash shells might tumble the ordering of the results - see the proposed solution by Stéphane for this.






    share|improve this answer



















    • 1




      who -l | ( tee >(head -n2 1>&3 ) | tail -n2 ) 3>&1 will ensure both head and tail write to stdout.
      – roaima
      Feb 23 '17 at 14:45










    • Depending on timing and on the number of logged-in users, tee may get a SIGPIPE when head terminates so that tee would not be able to write the full output to tail. Also, as head and tail run in parallel, the order of their output is not guaranteed.
      – Stéphane Chazelas
      Feb 23 '17 at 16:28










    • @StéphaneChazelas I can confirm the SIGPIPE problem simulating with a large enough seq as stdin - order is never tumbled, but tail does not show the last two number of the sequence. However I do not understand properly what is happening. Could you elaborate?
      – Fiximan
      Feb 23 '17 at 16:43






    • 1




      If head finishes before tee has written all the data to the pipe to head, then the next time tee write to that pipe it will get a SIGPIPE and die. tail will output the last two lines it has received before that.
      – Stéphane Chazelas
      Feb 23 '17 at 17:37






    • 1




      With bash, the order will be preserved because bash (at least current versions) process substitution leaves a bash process around with its stdout still connected to the pipe to tail waiting for the head command, so tail won't see eof until that process has terminated so not until head has terminated. Other shells like ksh93 or zsh don't have that limitation, so you may see the output tumbled there. One way to guarantee the order with any shell would be with tee >(head -n 2 3>&1 >&2) | tail -n 2. Here that fd 3 holds the pipe to tail open.
      – Stéphane Chazelas
      Feb 23 '17 at 18:00


















    up vote
    -1
    down vote













    It goes wrong for you when you are trying to use tail command. You are not actually using it to extract what you want from who command's output but from head command.



    If you need to use head and tail combined with piping, you could do it like this for example:



    (who |head -n5 && who |tail -n2)



    Modify numbers to meet your needs






    share|improve this answer



















    • 1




      This won't work because head will eat up all of stdin, leaving nothing for tail.
      – roaima
      Feb 23 '17 at 14:42










    • @roaima I see where it went wrong now. But there's one weird thing I don't uderstand at all. I tried to run following command ls -l |(head -n5 && tail -n2). I tested it 10 times now, 6 times I got the result I wanted and 4 times I didn't. Why is that?
      – jiipeezz
      Feb 23 '17 at 15:56






    • 1




      @jiipeezz, that very much depends on timing and the number of files in the directory. It depends whether the first read() done by head reads the whole output of ls -l or not.
      – Stéphane Chazelas
      Feb 23 '17 at 18:04










    • @StéphaneChazelas thanks for elaborating. Now it makes more sense. Corrected the answer to something that works.
      – jiipeezz
      Feb 23 '17 at 20:38











    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%2f346977%2fusing-pipes-to-list-the-first-two-and-last-two-users-on-the-system%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote













    Directly:



    who | head -2
    who | tail -2





    share|improve this answer

























      up vote
      4
      down vote













      Directly:



      who | head -2
      who | tail -2





      share|improve this answer























        up vote
        4
        down vote










        up vote
        4
        down vote









        Directly:



        who | head -2
        who | tail -2





        share|improve this answer












        Directly:



        who | head -2
        who | tail -2






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 23 '17 at 4:21









        Jeff Schaller

        36.6k1052121




        36.6k1052121
























            up vote
            0
            down vote













            You can use tee and a redirection to stderr



            who | tee >(head -n2 1>&2 ) | tail -n2


            Where tee allows you to copy stdin to both a file and stdout. Here we actually replace the file by the head command and redirect its output from stdout to stderr (via 1>&2) as all of stout is piped to the tail command and processed, so we need to circumvent tailing the head result, too. stderr itself is however still printed on the terminal.





            Updates following discussion in comments



            As pointed out by Stéphane, head might send a SIGPIPE signal kill the pipe prematurely so that tail might not see the actual end of the input stream.



            To prevent this, one could ignore the SIGPIPE signal as described here, by using a trap with an empty command. Tested in bash only .....



            who | { trap "" PIPE ; tee >( head -n 2 3>&1 >&2  ) ; } | tail -n 2


            Additionally mentioned in the comments: non-bash shells might tumble the ordering of the results - see the proposed solution by Stéphane for this.






            share|improve this answer



















            • 1




              who -l | ( tee >(head -n2 1>&3 ) | tail -n2 ) 3>&1 will ensure both head and tail write to stdout.
              – roaima
              Feb 23 '17 at 14:45










            • Depending on timing and on the number of logged-in users, tee may get a SIGPIPE when head terminates so that tee would not be able to write the full output to tail. Also, as head and tail run in parallel, the order of their output is not guaranteed.
              – Stéphane Chazelas
              Feb 23 '17 at 16:28










            • @StéphaneChazelas I can confirm the SIGPIPE problem simulating with a large enough seq as stdin - order is never tumbled, but tail does not show the last two number of the sequence. However I do not understand properly what is happening. Could you elaborate?
              – Fiximan
              Feb 23 '17 at 16:43






            • 1




              If head finishes before tee has written all the data to the pipe to head, then the next time tee write to that pipe it will get a SIGPIPE and die. tail will output the last two lines it has received before that.
              – Stéphane Chazelas
              Feb 23 '17 at 17:37






            • 1




              With bash, the order will be preserved because bash (at least current versions) process substitution leaves a bash process around with its stdout still connected to the pipe to tail waiting for the head command, so tail won't see eof until that process has terminated so not until head has terminated. Other shells like ksh93 or zsh don't have that limitation, so you may see the output tumbled there. One way to guarantee the order with any shell would be with tee >(head -n 2 3>&1 >&2) | tail -n 2. Here that fd 3 holds the pipe to tail open.
              – Stéphane Chazelas
              Feb 23 '17 at 18:00















            up vote
            0
            down vote













            You can use tee and a redirection to stderr



            who | tee >(head -n2 1>&2 ) | tail -n2


            Where tee allows you to copy stdin to both a file and stdout. Here we actually replace the file by the head command and redirect its output from stdout to stderr (via 1>&2) as all of stout is piped to the tail command and processed, so we need to circumvent tailing the head result, too. stderr itself is however still printed on the terminal.





            Updates following discussion in comments



            As pointed out by Stéphane, head might send a SIGPIPE signal kill the pipe prematurely so that tail might not see the actual end of the input stream.



            To prevent this, one could ignore the SIGPIPE signal as described here, by using a trap with an empty command. Tested in bash only .....



            who | { trap "" PIPE ; tee >( head -n 2 3>&1 >&2  ) ; } | tail -n 2


            Additionally mentioned in the comments: non-bash shells might tumble the ordering of the results - see the proposed solution by Stéphane for this.






            share|improve this answer



















            • 1




              who -l | ( tee >(head -n2 1>&3 ) | tail -n2 ) 3>&1 will ensure both head and tail write to stdout.
              – roaima
              Feb 23 '17 at 14:45










            • Depending on timing and on the number of logged-in users, tee may get a SIGPIPE when head terminates so that tee would not be able to write the full output to tail. Also, as head and tail run in parallel, the order of their output is not guaranteed.
              – Stéphane Chazelas
              Feb 23 '17 at 16:28










            • @StéphaneChazelas I can confirm the SIGPIPE problem simulating with a large enough seq as stdin - order is never tumbled, but tail does not show the last two number of the sequence. However I do not understand properly what is happening. Could you elaborate?
              – Fiximan
              Feb 23 '17 at 16:43






            • 1




              If head finishes before tee has written all the data to the pipe to head, then the next time tee write to that pipe it will get a SIGPIPE and die. tail will output the last two lines it has received before that.
              – Stéphane Chazelas
              Feb 23 '17 at 17:37






            • 1




              With bash, the order will be preserved because bash (at least current versions) process substitution leaves a bash process around with its stdout still connected to the pipe to tail waiting for the head command, so tail won't see eof until that process has terminated so not until head has terminated. Other shells like ksh93 or zsh don't have that limitation, so you may see the output tumbled there. One way to guarantee the order with any shell would be with tee >(head -n 2 3>&1 >&2) | tail -n 2. Here that fd 3 holds the pipe to tail open.
              – Stéphane Chazelas
              Feb 23 '17 at 18:00













            up vote
            0
            down vote










            up vote
            0
            down vote









            You can use tee and a redirection to stderr



            who | tee >(head -n2 1>&2 ) | tail -n2


            Where tee allows you to copy stdin to both a file and stdout. Here we actually replace the file by the head command and redirect its output from stdout to stderr (via 1>&2) as all of stout is piped to the tail command and processed, so we need to circumvent tailing the head result, too. stderr itself is however still printed on the terminal.





            Updates following discussion in comments



            As pointed out by Stéphane, head might send a SIGPIPE signal kill the pipe prematurely so that tail might not see the actual end of the input stream.



            To prevent this, one could ignore the SIGPIPE signal as described here, by using a trap with an empty command. Tested in bash only .....



            who | { trap "" PIPE ; tee >( head -n 2 3>&1 >&2  ) ; } | tail -n 2


            Additionally mentioned in the comments: non-bash shells might tumble the ordering of the results - see the proposed solution by Stéphane for this.






            share|improve this answer














            You can use tee and a redirection to stderr



            who | tee >(head -n2 1>&2 ) | tail -n2


            Where tee allows you to copy stdin to both a file and stdout. Here we actually replace the file by the head command and redirect its output from stdout to stderr (via 1>&2) as all of stout is piped to the tail command and processed, so we need to circumvent tailing the head result, too. stderr itself is however still printed on the terminal.





            Updates following discussion in comments



            As pointed out by Stéphane, head might send a SIGPIPE signal kill the pipe prematurely so that tail might not see the actual end of the input stream.



            To prevent this, one could ignore the SIGPIPE signal as described here, by using a trap with an empty command. Tested in bash only .....



            who | { trap "" PIPE ; tee >( head -n 2 3>&1 >&2  ) ; } | tail -n 2


            Additionally mentioned in the comments: non-bash shells might tumble the ordering of the results - see the proposed solution by Stéphane for this.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 13 '17 at 12:36









            Community

            1




            1










            answered Feb 23 '17 at 14:19









            Fiximan

            3,143524




            3,143524








            • 1




              who -l | ( tee >(head -n2 1>&3 ) | tail -n2 ) 3>&1 will ensure both head and tail write to stdout.
              – roaima
              Feb 23 '17 at 14:45










            • Depending on timing and on the number of logged-in users, tee may get a SIGPIPE when head terminates so that tee would not be able to write the full output to tail. Also, as head and tail run in parallel, the order of their output is not guaranteed.
              – Stéphane Chazelas
              Feb 23 '17 at 16:28










            • @StéphaneChazelas I can confirm the SIGPIPE problem simulating with a large enough seq as stdin - order is never tumbled, but tail does not show the last two number of the sequence. However I do not understand properly what is happening. Could you elaborate?
              – Fiximan
              Feb 23 '17 at 16:43






            • 1




              If head finishes before tee has written all the data to the pipe to head, then the next time tee write to that pipe it will get a SIGPIPE and die. tail will output the last two lines it has received before that.
              – Stéphane Chazelas
              Feb 23 '17 at 17:37






            • 1




              With bash, the order will be preserved because bash (at least current versions) process substitution leaves a bash process around with its stdout still connected to the pipe to tail waiting for the head command, so tail won't see eof until that process has terminated so not until head has terminated. Other shells like ksh93 or zsh don't have that limitation, so you may see the output tumbled there. One way to guarantee the order with any shell would be with tee >(head -n 2 3>&1 >&2) | tail -n 2. Here that fd 3 holds the pipe to tail open.
              – Stéphane Chazelas
              Feb 23 '17 at 18:00














            • 1




              who -l | ( tee >(head -n2 1>&3 ) | tail -n2 ) 3>&1 will ensure both head and tail write to stdout.
              – roaima
              Feb 23 '17 at 14:45










            • Depending on timing and on the number of logged-in users, tee may get a SIGPIPE when head terminates so that tee would not be able to write the full output to tail. Also, as head and tail run in parallel, the order of their output is not guaranteed.
              – Stéphane Chazelas
              Feb 23 '17 at 16:28










            • @StéphaneChazelas I can confirm the SIGPIPE problem simulating with a large enough seq as stdin - order is never tumbled, but tail does not show the last two number of the sequence. However I do not understand properly what is happening. Could you elaborate?
              – Fiximan
              Feb 23 '17 at 16:43






            • 1




              If head finishes before tee has written all the data to the pipe to head, then the next time tee write to that pipe it will get a SIGPIPE and die. tail will output the last two lines it has received before that.
              – Stéphane Chazelas
              Feb 23 '17 at 17:37






            • 1




              With bash, the order will be preserved because bash (at least current versions) process substitution leaves a bash process around with its stdout still connected to the pipe to tail waiting for the head command, so tail won't see eof until that process has terminated so not until head has terminated. Other shells like ksh93 or zsh don't have that limitation, so you may see the output tumbled there. One way to guarantee the order with any shell would be with tee >(head -n 2 3>&1 >&2) | tail -n 2. Here that fd 3 holds the pipe to tail open.
              – Stéphane Chazelas
              Feb 23 '17 at 18:00








            1




            1




            who -l | ( tee >(head -n2 1>&3 ) | tail -n2 ) 3>&1 will ensure both head and tail write to stdout.
            – roaima
            Feb 23 '17 at 14:45




            who -l | ( tee >(head -n2 1>&3 ) | tail -n2 ) 3>&1 will ensure both head and tail write to stdout.
            – roaima
            Feb 23 '17 at 14:45












            Depending on timing and on the number of logged-in users, tee may get a SIGPIPE when head terminates so that tee would not be able to write the full output to tail. Also, as head and tail run in parallel, the order of their output is not guaranteed.
            – Stéphane Chazelas
            Feb 23 '17 at 16:28




            Depending on timing and on the number of logged-in users, tee may get a SIGPIPE when head terminates so that tee would not be able to write the full output to tail. Also, as head and tail run in parallel, the order of their output is not guaranteed.
            – Stéphane Chazelas
            Feb 23 '17 at 16:28












            @StéphaneChazelas I can confirm the SIGPIPE problem simulating with a large enough seq as stdin - order is never tumbled, but tail does not show the last two number of the sequence. However I do not understand properly what is happening. Could you elaborate?
            – Fiximan
            Feb 23 '17 at 16:43




            @StéphaneChazelas I can confirm the SIGPIPE problem simulating with a large enough seq as stdin - order is never tumbled, but tail does not show the last two number of the sequence. However I do not understand properly what is happening. Could you elaborate?
            – Fiximan
            Feb 23 '17 at 16:43




            1




            1




            If head finishes before tee has written all the data to the pipe to head, then the next time tee write to that pipe it will get a SIGPIPE and die. tail will output the last two lines it has received before that.
            – Stéphane Chazelas
            Feb 23 '17 at 17:37




            If head finishes before tee has written all the data to the pipe to head, then the next time tee write to that pipe it will get a SIGPIPE and die. tail will output the last two lines it has received before that.
            – Stéphane Chazelas
            Feb 23 '17 at 17:37




            1




            1




            With bash, the order will be preserved because bash (at least current versions) process substitution leaves a bash process around with its stdout still connected to the pipe to tail waiting for the head command, so tail won't see eof until that process has terminated so not until head has terminated. Other shells like ksh93 or zsh don't have that limitation, so you may see the output tumbled there. One way to guarantee the order with any shell would be with tee >(head -n 2 3>&1 >&2) | tail -n 2. Here that fd 3 holds the pipe to tail open.
            – Stéphane Chazelas
            Feb 23 '17 at 18:00




            With bash, the order will be preserved because bash (at least current versions) process substitution leaves a bash process around with its stdout still connected to the pipe to tail waiting for the head command, so tail won't see eof until that process has terminated so not until head has terminated. Other shells like ksh93 or zsh don't have that limitation, so you may see the output tumbled there. One way to guarantee the order with any shell would be with tee >(head -n 2 3>&1 >&2) | tail -n 2. Here that fd 3 holds the pipe to tail open.
            – Stéphane Chazelas
            Feb 23 '17 at 18:00










            up vote
            -1
            down vote













            It goes wrong for you when you are trying to use tail command. You are not actually using it to extract what you want from who command's output but from head command.



            If you need to use head and tail combined with piping, you could do it like this for example:



            (who |head -n5 && who |tail -n2)



            Modify numbers to meet your needs






            share|improve this answer



















            • 1




              This won't work because head will eat up all of stdin, leaving nothing for tail.
              – roaima
              Feb 23 '17 at 14:42










            • @roaima I see where it went wrong now. But there's one weird thing I don't uderstand at all. I tried to run following command ls -l |(head -n5 && tail -n2). I tested it 10 times now, 6 times I got the result I wanted and 4 times I didn't. Why is that?
              – jiipeezz
              Feb 23 '17 at 15:56






            • 1




              @jiipeezz, that very much depends on timing and the number of files in the directory. It depends whether the first read() done by head reads the whole output of ls -l or not.
              – Stéphane Chazelas
              Feb 23 '17 at 18:04










            • @StéphaneChazelas thanks for elaborating. Now it makes more sense. Corrected the answer to something that works.
              – jiipeezz
              Feb 23 '17 at 20:38















            up vote
            -1
            down vote













            It goes wrong for you when you are trying to use tail command. You are not actually using it to extract what you want from who command's output but from head command.



            If you need to use head and tail combined with piping, you could do it like this for example:



            (who |head -n5 && who |tail -n2)



            Modify numbers to meet your needs






            share|improve this answer



















            • 1




              This won't work because head will eat up all of stdin, leaving nothing for tail.
              – roaima
              Feb 23 '17 at 14:42










            • @roaima I see where it went wrong now. But there's one weird thing I don't uderstand at all. I tried to run following command ls -l |(head -n5 && tail -n2). I tested it 10 times now, 6 times I got the result I wanted and 4 times I didn't. Why is that?
              – jiipeezz
              Feb 23 '17 at 15:56






            • 1




              @jiipeezz, that very much depends on timing and the number of files in the directory. It depends whether the first read() done by head reads the whole output of ls -l or not.
              – Stéphane Chazelas
              Feb 23 '17 at 18:04










            • @StéphaneChazelas thanks for elaborating. Now it makes more sense. Corrected the answer to something that works.
              – jiipeezz
              Feb 23 '17 at 20:38













            up vote
            -1
            down vote










            up vote
            -1
            down vote









            It goes wrong for you when you are trying to use tail command. You are not actually using it to extract what you want from who command's output but from head command.



            If you need to use head and tail combined with piping, you could do it like this for example:



            (who |head -n5 && who |tail -n2)



            Modify numbers to meet your needs






            share|improve this answer














            It goes wrong for you when you are trying to use tail command. You are not actually using it to extract what you want from who command's output but from head command.



            If you need to use head and tail combined with piping, you could do it like this for example:



            (who |head -n5 && who |tail -n2)



            Modify numbers to meet your needs







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 23 '17 at 16:02

























            answered Feb 23 '17 at 3:05









            jiipeezz

            21125




            21125








            • 1




              This won't work because head will eat up all of stdin, leaving nothing for tail.
              – roaima
              Feb 23 '17 at 14:42










            • @roaima I see where it went wrong now. But there's one weird thing I don't uderstand at all. I tried to run following command ls -l |(head -n5 && tail -n2). I tested it 10 times now, 6 times I got the result I wanted and 4 times I didn't. Why is that?
              – jiipeezz
              Feb 23 '17 at 15:56






            • 1




              @jiipeezz, that very much depends on timing and the number of files in the directory. It depends whether the first read() done by head reads the whole output of ls -l or not.
              – Stéphane Chazelas
              Feb 23 '17 at 18:04










            • @StéphaneChazelas thanks for elaborating. Now it makes more sense. Corrected the answer to something that works.
              – jiipeezz
              Feb 23 '17 at 20:38














            • 1




              This won't work because head will eat up all of stdin, leaving nothing for tail.
              – roaima
              Feb 23 '17 at 14:42










            • @roaima I see where it went wrong now. But there's one weird thing I don't uderstand at all. I tried to run following command ls -l |(head -n5 && tail -n2). I tested it 10 times now, 6 times I got the result I wanted and 4 times I didn't. Why is that?
              – jiipeezz
              Feb 23 '17 at 15:56






            • 1




              @jiipeezz, that very much depends on timing and the number of files in the directory. It depends whether the first read() done by head reads the whole output of ls -l or not.
              – Stéphane Chazelas
              Feb 23 '17 at 18:04










            • @StéphaneChazelas thanks for elaborating. Now it makes more sense. Corrected the answer to something that works.
              – jiipeezz
              Feb 23 '17 at 20:38








            1




            1




            This won't work because head will eat up all of stdin, leaving nothing for tail.
            – roaima
            Feb 23 '17 at 14:42




            This won't work because head will eat up all of stdin, leaving nothing for tail.
            – roaima
            Feb 23 '17 at 14:42












            @roaima I see where it went wrong now. But there's one weird thing I don't uderstand at all. I tried to run following command ls -l |(head -n5 && tail -n2). I tested it 10 times now, 6 times I got the result I wanted and 4 times I didn't. Why is that?
            – jiipeezz
            Feb 23 '17 at 15:56




            @roaima I see where it went wrong now. But there's one weird thing I don't uderstand at all. I tried to run following command ls -l |(head -n5 && tail -n2). I tested it 10 times now, 6 times I got the result I wanted and 4 times I didn't. Why is that?
            – jiipeezz
            Feb 23 '17 at 15:56




            1




            1




            @jiipeezz, that very much depends on timing and the number of files in the directory. It depends whether the first read() done by head reads the whole output of ls -l or not.
            – Stéphane Chazelas
            Feb 23 '17 at 18:04




            @jiipeezz, that very much depends on timing and the number of files in the directory. It depends whether the first read() done by head reads the whole output of ls -l or not.
            – Stéphane Chazelas
            Feb 23 '17 at 18:04












            @StéphaneChazelas thanks for elaborating. Now it makes more sense. Corrected the answer to something that works.
            – jiipeezz
            Feb 23 '17 at 20:38




            @StéphaneChazelas thanks for elaborating. Now it makes more sense. Corrected the answer to something that works.
            – jiipeezz
            Feb 23 '17 at 20:38


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f346977%2fusing-pipes-to-list-the-first-two-and-last-two-users-on-the-system%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

            サソリ

            広島県道265号伴広島線

            Accessing regular linux commands in Huawei's Dopra Linux