Using The Grep Command












0














I have two simple questions regarding the use of the grep command.



What I want to do is:




  1. show the total lines (processes) running as me except for the command(s) that we used to create the listing.

  2. Next, I want to modify a listing of total lines running as me and only parse out the userID and the PID into a new list.


For the first one, I thought something along the lines of
(ps -ef | grep myusername | grep -v "somecommand" | wc -l)
would suffice but I'm not sure what to type in for someprocess to edit out the command used to generate the list.



For the second question, I'm not sure how to approach it.










share|improve this question
























  • You can tell ps to only show processes belonging to a certain user, no need to use grep for that. Also investigate the ps --format option for formatting the output according to your requirements.
    – wurtel
    Feb 17 '15 at 10:12










  • Well, (ps -ef | grep myusername) worked for grabbing just the processes under my username.
    – Wesley Jordt
    Feb 17 '15 at 10:20










  • Sure, but is it the best solution... or is it what the homework assignment is asking for :-)
    – wurtel
    Feb 17 '15 at 10:30










  • It is what I was taught, to be honest. Haha.
    – Wesley Jordt
    Feb 17 '15 at 10:34
















0














I have two simple questions regarding the use of the grep command.



What I want to do is:




  1. show the total lines (processes) running as me except for the command(s) that we used to create the listing.

  2. Next, I want to modify a listing of total lines running as me and only parse out the userID and the PID into a new list.


For the first one, I thought something along the lines of
(ps -ef | grep myusername | grep -v "somecommand" | wc -l)
would suffice but I'm not sure what to type in for someprocess to edit out the command used to generate the list.



For the second question, I'm not sure how to approach it.










share|improve this question
























  • You can tell ps to only show processes belonging to a certain user, no need to use grep for that. Also investigate the ps --format option for formatting the output according to your requirements.
    – wurtel
    Feb 17 '15 at 10:12










  • Well, (ps -ef | grep myusername) worked for grabbing just the processes under my username.
    – Wesley Jordt
    Feb 17 '15 at 10:20










  • Sure, but is it the best solution... or is it what the homework assignment is asking for :-)
    – wurtel
    Feb 17 '15 at 10:30










  • It is what I was taught, to be honest. Haha.
    – Wesley Jordt
    Feb 17 '15 at 10:34














0












0








0







I have two simple questions regarding the use of the grep command.



What I want to do is:




  1. show the total lines (processes) running as me except for the command(s) that we used to create the listing.

  2. Next, I want to modify a listing of total lines running as me and only parse out the userID and the PID into a new list.


For the first one, I thought something along the lines of
(ps -ef | grep myusername | grep -v "somecommand" | wc -l)
would suffice but I'm not sure what to type in for someprocess to edit out the command used to generate the list.



For the second question, I'm not sure how to approach it.










share|improve this question















I have two simple questions regarding the use of the grep command.



What I want to do is:




  1. show the total lines (processes) running as me except for the command(s) that we used to create the listing.

  2. Next, I want to modify a listing of total lines running as me and only parse out the userID and the PID into a new list.


For the first one, I thought something along the lines of
(ps -ef | grep myusername | grep -v "somecommand" | wc -l)
would suffice but I'm not sure what to type in for someprocess to edit out the command used to generate the list.



For the second question, I'm not sure how to approach it.







grep process






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 15 at 21:57









Rui F Ribeiro

38.9k1479129




38.9k1479129










asked Feb 17 '15 at 10:04









Wesley Jordt

106




106












  • You can tell ps to only show processes belonging to a certain user, no need to use grep for that. Also investigate the ps --format option for formatting the output according to your requirements.
    – wurtel
    Feb 17 '15 at 10:12










  • Well, (ps -ef | grep myusername) worked for grabbing just the processes under my username.
    – Wesley Jordt
    Feb 17 '15 at 10:20










  • Sure, but is it the best solution... or is it what the homework assignment is asking for :-)
    – wurtel
    Feb 17 '15 at 10:30










  • It is what I was taught, to be honest. Haha.
    – Wesley Jordt
    Feb 17 '15 at 10:34


















  • You can tell ps to only show processes belonging to a certain user, no need to use grep for that. Also investigate the ps --format option for formatting the output according to your requirements.
    – wurtel
    Feb 17 '15 at 10:12










  • Well, (ps -ef | grep myusername) worked for grabbing just the processes under my username.
    – Wesley Jordt
    Feb 17 '15 at 10:20










  • Sure, but is it the best solution... or is it what the homework assignment is asking for :-)
    – wurtel
    Feb 17 '15 at 10:30










  • It is what I was taught, to be honest. Haha.
    – Wesley Jordt
    Feb 17 '15 at 10:34
















You can tell ps to only show processes belonging to a certain user, no need to use grep for that. Also investigate the ps --format option for formatting the output according to your requirements.
– wurtel
Feb 17 '15 at 10:12




You can tell ps to only show processes belonging to a certain user, no need to use grep for that. Also investigate the ps --format option for formatting the output according to your requirements.
– wurtel
Feb 17 '15 at 10:12












Well, (ps -ef | grep myusername) worked for grabbing just the processes under my username.
– Wesley Jordt
Feb 17 '15 at 10:20




Well, (ps -ef | grep myusername) worked for grabbing just the processes under my username.
– Wesley Jordt
Feb 17 '15 at 10:20












Sure, but is it the best solution... or is it what the homework assignment is asking for :-)
– wurtel
Feb 17 '15 at 10:30




Sure, but is it the best solution... or is it what the homework assignment is asking for :-)
– wurtel
Feb 17 '15 at 10:30












It is what I was taught, to be honest. Haha.
– Wesley Jordt
Feb 17 '15 at 10:34




It is what I was taught, to be honest. Haha.
– Wesley Jordt
Feb 17 '15 at 10:34










3 Answers
3






active

oldest

votes


















0














To list all the processes by an user except for the command(s) that we used to create the listing, you can use:



ps -U username -u username u | grep -v ps


It list all the processes started by username as real user ID and effective user ID and filter the process ps to avoid to list the command used to create the listing.



The other one (user and PID):



ps -U username -u username u | grep -v ps | awk  {'print $1, $2'}


awk is used to to only show columns 1 and 2 of the results.






share|improve this answer























  • Excellent! I have yet to learn about the awk command unfortunately but I suppo se now is the best time! Could you perhaps briefly explain the logic of what is going on in the first one? I see that ps -U username -u username u lists every process running as username, I also see that grep -v selects all of the nonmatching lines to the ps command. Does that indicate that there is a process with ps in its name? Thanx!
    – Wesley Jordt
    Feb 17 '15 at 10:33










  • wc -l counts all the lines including the title. (PID TTY TIME CMD)
    – jcbermu
    Feb 17 '15 at 10:52










  • THanx for the awk command explanation! You're correct. I forgot to add it into the comment. When I enter ps -u username | grep -v ps | wc -l into the command line it spits out that I have 7 processes. When I enter your command with | wc -l on the back, it returns 6. Could you enlighten me please? Haha.
    – Wesley Jordt
    Feb 17 '15 at 10:57












  • You have to use -U username -u username to have the real and effective UID. Normally are the same. However if a program with a set-uid bit set is run, while the real UID remains that of the user who ran it, the effective UID is that of the user who owns the file.
    – jcbermu
    Feb 17 '15 at 11:18



















0














For the first question you can use ps with below option to list all process that are run with your username.



$ ps -o pid,uid -u 1000|wc -l


You can replace the value 1000 with the UID of your user. I have added only pid and uid to get displayed. you can any any more of them.



For the second case is same as the first



$  ps -o pid,uid -u 1000 > process.list





share|improve this answer





























    0














    For your first question, you can use the -c command to grep, which gives you a count of matches.



    ps -ef | grep -v "grep" | grep -c "username"


    This lists all processes, then removes the grep itself (don't count the one we are using to get the listing) and then get's a total count that are from your username.






    share|improve this answer





















    • For me, the ps command does not show up in my ps results. If it does for you, then you can add an additional grep -v "ps". It's usually a good habit to put what you are grepping for in quotes, so that when you grep for something with spaces or other characters, it will handle them correctly.
      – jrel
      Feb 17 '15 at 14:24











    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%2f185269%2fusing-the-grep-command%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









    0














    To list all the processes by an user except for the command(s) that we used to create the listing, you can use:



    ps -U username -u username u | grep -v ps


    It list all the processes started by username as real user ID and effective user ID and filter the process ps to avoid to list the command used to create the listing.



    The other one (user and PID):



    ps -U username -u username u | grep -v ps | awk  {'print $1, $2'}


    awk is used to to only show columns 1 and 2 of the results.






    share|improve this answer























    • Excellent! I have yet to learn about the awk command unfortunately but I suppo se now is the best time! Could you perhaps briefly explain the logic of what is going on in the first one? I see that ps -U username -u username u lists every process running as username, I also see that grep -v selects all of the nonmatching lines to the ps command. Does that indicate that there is a process with ps in its name? Thanx!
      – Wesley Jordt
      Feb 17 '15 at 10:33










    • wc -l counts all the lines including the title. (PID TTY TIME CMD)
      – jcbermu
      Feb 17 '15 at 10:52










    • THanx for the awk command explanation! You're correct. I forgot to add it into the comment. When I enter ps -u username | grep -v ps | wc -l into the command line it spits out that I have 7 processes. When I enter your command with | wc -l on the back, it returns 6. Could you enlighten me please? Haha.
      – Wesley Jordt
      Feb 17 '15 at 10:57












    • You have to use -U username -u username to have the real and effective UID. Normally are the same. However if a program with a set-uid bit set is run, while the real UID remains that of the user who ran it, the effective UID is that of the user who owns the file.
      – jcbermu
      Feb 17 '15 at 11:18
















    0














    To list all the processes by an user except for the command(s) that we used to create the listing, you can use:



    ps -U username -u username u | grep -v ps


    It list all the processes started by username as real user ID and effective user ID and filter the process ps to avoid to list the command used to create the listing.



    The other one (user and PID):



    ps -U username -u username u | grep -v ps | awk  {'print $1, $2'}


    awk is used to to only show columns 1 and 2 of the results.






    share|improve this answer























    • Excellent! I have yet to learn about the awk command unfortunately but I suppo se now is the best time! Could you perhaps briefly explain the logic of what is going on in the first one? I see that ps -U username -u username u lists every process running as username, I also see that grep -v selects all of the nonmatching lines to the ps command. Does that indicate that there is a process with ps in its name? Thanx!
      – Wesley Jordt
      Feb 17 '15 at 10:33










    • wc -l counts all the lines including the title. (PID TTY TIME CMD)
      – jcbermu
      Feb 17 '15 at 10:52










    • THanx for the awk command explanation! You're correct. I forgot to add it into the comment. When I enter ps -u username | grep -v ps | wc -l into the command line it spits out that I have 7 processes. When I enter your command with | wc -l on the back, it returns 6. Could you enlighten me please? Haha.
      – Wesley Jordt
      Feb 17 '15 at 10:57












    • You have to use -U username -u username to have the real and effective UID. Normally are the same. However if a program with a set-uid bit set is run, while the real UID remains that of the user who ran it, the effective UID is that of the user who owns the file.
      – jcbermu
      Feb 17 '15 at 11:18














    0












    0








    0






    To list all the processes by an user except for the command(s) that we used to create the listing, you can use:



    ps -U username -u username u | grep -v ps


    It list all the processes started by username as real user ID and effective user ID and filter the process ps to avoid to list the command used to create the listing.



    The other one (user and PID):



    ps -U username -u username u | grep -v ps | awk  {'print $1, $2'}


    awk is used to to only show columns 1 and 2 of the results.






    share|improve this answer














    To list all the processes by an user except for the command(s) that we used to create the listing, you can use:



    ps -U username -u username u | grep -v ps


    It list all the processes started by username as real user ID and effective user ID and filter the process ps to avoid to list the command used to create the listing.



    The other one (user and PID):



    ps -U username -u username u | grep -v ps | awk  {'print $1, $2'}


    awk is used to to only show columns 1 and 2 of the results.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 17 '15 at 10:50

























    answered Feb 17 '15 at 10:19









    jcbermu

    3,312819




    3,312819












    • Excellent! I have yet to learn about the awk command unfortunately but I suppo se now is the best time! Could you perhaps briefly explain the logic of what is going on in the first one? I see that ps -U username -u username u lists every process running as username, I also see that grep -v selects all of the nonmatching lines to the ps command. Does that indicate that there is a process with ps in its name? Thanx!
      – Wesley Jordt
      Feb 17 '15 at 10:33










    • wc -l counts all the lines including the title. (PID TTY TIME CMD)
      – jcbermu
      Feb 17 '15 at 10:52










    • THanx for the awk command explanation! You're correct. I forgot to add it into the comment. When I enter ps -u username | grep -v ps | wc -l into the command line it spits out that I have 7 processes. When I enter your command with | wc -l on the back, it returns 6. Could you enlighten me please? Haha.
      – Wesley Jordt
      Feb 17 '15 at 10:57












    • You have to use -U username -u username to have the real and effective UID. Normally are the same. However if a program with a set-uid bit set is run, while the real UID remains that of the user who ran it, the effective UID is that of the user who owns the file.
      – jcbermu
      Feb 17 '15 at 11:18


















    • Excellent! I have yet to learn about the awk command unfortunately but I suppo se now is the best time! Could you perhaps briefly explain the logic of what is going on in the first one? I see that ps -U username -u username u lists every process running as username, I also see that grep -v selects all of the nonmatching lines to the ps command. Does that indicate that there is a process with ps in its name? Thanx!
      – Wesley Jordt
      Feb 17 '15 at 10:33










    • wc -l counts all the lines including the title. (PID TTY TIME CMD)
      – jcbermu
      Feb 17 '15 at 10:52










    • THanx for the awk command explanation! You're correct. I forgot to add it into the comment. When I enter ps -u username | grep -v ps | wc -l into the command line it spits out that I have 7 processes. When I enter your command with | wc -l on the back, it returns 6. Could you enlighten me please? Haha.
      – Wesley Jordt
      Feb 17 '15 at 10:57












    • You have to use -U username -u username to have the real and effective UID. Normally are the same. However if a program with a set-uid bit set is run, while the real UID remains that of the user who ran it, the effective UID is that of the user who owns the file.
      – jcbermu
      Feb 17 '15 at 11:18
















    Excellent! I have yet to learn about the awk command unfortunately but I suppo se now is the best time! Could you perhaps briefly explain the logic of what is going on in the first one? I see that ps -U username -u username u lists every process running as username, I also see that grep -v selects all of the nonmatching lines to the ps command. Does that indicate that there is a process with ps in its name? Thanx!
    – Wesley Jordt
    Feb 17 '15 at 10:33




    Excellent! I have yet to learn about the awk command unfortunately but I suppo se now is the best time! Could you perhaps briefly explain the logic of what is going on in the first one? I see that ps -U username -u username u lists every process running as username, I also see that grep -v selects all of the nonmatching lines to the ps command. Does that indicate that there is a process with ps in its name? Thanx!
    – Wesley Jordt
    Feb 17 '15 at 10:33












    wc -l counts all the lines including the title. (PID TTY TIME CMD)
    – jcbermu
    Feb 17 '15 at 10:52




    wc -l counts all the lines including the title. (PID TTY TIME CMD)
    – jcbermu
    Feb 17 '15 at 10:52












    THanx for the awk command explanation! You're correct. I forgot to add it into the comment. When I enter ps -u username | grep -v ps | wc -l into the command line it spits out that I have 7 processes. When I enter your command with | wc -l on the back, it returns 6. Could you enlighten me please? Haha.
    – Wesley Jordt
    Feb 17 '15 at 10:57






    THanx for the awk command explanation! You're correct. I forgot to add it into the comment. When I enter ps -u username | grep -v ps | wc -l into the command line it spits out that I have 7 processes. When I enter your command with | wc -l on the back, it returns 6. Could you enlighten me please? Haha.
    – Wesley Jordt
    Feb 17 '15 at 10:57














    You have to use -U username -u username to have the real and effective UID. Normally are the same. However if a program with a set-uid bit set is run, while the real UID remains that of the user who ran it, the effective UID is that of the user who owns the file.
    – jcbermu
    Feb 17 '15 at 11:18




    You have to use -U username -u username to have the real and effective UID. Normally are the same. However if a program with a set-uid bit set is run, while the real UID remains that of the user who ran it, the effective UID is that of the user who owns the file.
    – jcbermu
    Feb 17 '15 at 11:18













    0














    For the first question you can use ps with below option to list all process that are run with your username.



    $ ps -o pid,uid -u 1000|wc -l


    You can replace the value 1000 with the UID of your user. I have added only pid and uid to get displayed. you can any any more of them.



    For the second case is same as the first



    $  ps -o pid,uid -u 1000 > process.list





    share|improve this answer


























      0














      For the first question you can use ps with below option to list all process that are run with your username.



      $ ps -o pid,uid -u 1000|wc -l


      You can replace the value 1000 with the UID of your user. I have added only pid and uid to get displayed. you can any any more of them.



      For the second case is same as the first



      $  ps -o pid,uid -u 1000 > process.list





      share|improve this answer
























        0












        0








        0






        For the first question you can use ps with below option to list all process that are run with your username.



        $ ps -o pid,uid -u 1000|wc -l


        You can replace the value 1000 with the UID of your user. I have added only pid and uid to get displayed. you can any any more of them.



        For the second case is same as the first



        $  ps -o pid,uid -u 1000 > process.list





        share|improve this answer












        For the first question you can use ps with below option to list all process that are run with your username.



        $ ps -o pid,uid -u 1000|wc -l


        You can replace the value 1000 with the UID of your user. I have added only pid and uid to get displayed. you can any any more of them.



        For the second case is same as the first



        $  ps -o pid,uid -u 1000 > process.list






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 17 '15 at 10:21









        Kannan Mohan

        2,02511016




        2,02511016























            0














            For your first question, you can use the -c command to grep, which gives you a count of matches.



            ps -ef | grep -v "grep" | grep -c "username"


            This lists all processes, then removes the grep itself (don't count the one we are using to get the listing) and then get's a total count that are from your username.






            share|improve this answer





















            • For me, the ps command does not show up in my ps results. If it does for you, then you can add an additional grep -v "ps". It's usually a good habit to put what you are grepping for in quotes, so that when you grep for something with spaces or other characters, it will handle them correctly.
              – jrel
              Feb 17 '15 at 14:24
















            0














            For your first question, you can use the -c command to grep, which gives you a count of matches.



            ps -ef | grep -v "grep" | grep -c "username"


            This lists all processes, then removes the grep itself (don't count the one we are using to get the listing) and then get's a total count that are from your username.






            share|improve this answer





















            • For me, the ps command does not show up in my ps results. If it does for you, then you can add an additional grep -v "ps". It's usually a good habit to put what you are grepping for in quotes, so that when you grep for something with spaces or other characters, it will handle them correctly.
              – jrel
              Feb 17 '15 at 14:24














            0












            0








            0






            For your first question, you can use the -c command to grep, which gives you a count of matches.



            ps -ef | grep -v "grep" | grep -c "username"


            This lists all processes, then removes the grep itself (don't count the one we are using to get the listing) and then get's a total count that are from your username.






            share|improve this answer












            For your first question, you can use the -c command to grep, which gives you a count of matches.



            ps -ef | grep -v "grep" | grep -c "username"


            This lists all processes, then removes the grep itself (don't count the one we are using to get the listing) and then get's a total count that are from your username.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 17 '15 at 14:21









            jrel

            3716




            3716












            • For me, the ps command does not show up in my ps results. If it does for you, then you can add an additional grep -v "ps". It's usually a good habit to put what you are grepping for in quotes, so that when you grep for something with spaces or other characters, it will handle them correctly.
              – jrel
              Feb 17 '15 at 14:24


















            • For me, the ps command does not show up in my ps results. If it does for you, then you can add an additional grep -v "ps". It's usually a good habit to put what you are grepping for in quotes, so that when you grep for something with spaces or other characters, it will handle them correctly.
              – jrel
              Feb 17 '15 at 14:24
















            For me, the ps command does not show up in my ps results. If it does for you, then you can add an additional grep -v "ps". It's usually a good habit to put what you are grepping for in quotes, so that when you grep for something with spaces or other characters, it will handle them correctly.
            – jrel
            Feb 17 '15 at 14:24




            For me, the ps command does not show up in my ps results. If it does for you, then you can add an additional grep -v "ps". It's usually a good habit to put what you are grepping for in quotes, so that when you grep for something with spaces or other characters, it will handle them correctly.
            – jrel
            Feb 17 '15 at 14:24


















            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%2f185269%2fusing-the-grep-command%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

            Morgemoulin

            Scott Moir

            Souastre