awk function with a number parameter for the column you want to print












2















I want to use my awk shortcut as a function, so that I can pass the column number which then prints me the output. My aliases are:



alias A="| awk '{print $1}'
alias G="| grep -i'


Instad of typing:



ps -ef | grep mysql | awk 'print $2'


I want to be able to type this:



ps -ef G mysql A 2


Any suggestions?










share|improve this question




















  • 4





    For a completely different approach, try pgrep -f mysql (assuming pgrep is available).

    – jw013
    Jul 27 '12 at 17:04













  • nice tool thx!!

    – DannyRe
    Jul 27 '12 at 17:13











  • Indeed. And for arbitrary fields, ps -o <field> $(pgrep mysql).

    – Mikel
    Jul 27 '12 at 17:22






  • 1





    or ps h -o %p -C mysqld if you want the PIDs of a particular named process (-C is exact match, not search pattern or regexp). You can have multiple -C args, e.g. ps h -o %p -C mysqld -C mysql to get client and server processes.

    – cas
    Jul 27 '12 at 21:06
















2















I want to use my awk shortcut as a function, so that I can pass the column number which then prints me the output. My aliases are:



alias A="| awk '{print $1}'
alias G="| grep -i'


Instad of typing:



ps -ef | grep mysql | awk 'print $2'


I want to be able to type this:



ps -ef G mysql A 2


Any suggestions?










share|improve this question




















  • 4





    For a completely different approach, try pgrep -f mysql (assuming pgrep is available).

    – jw013
    Jul 27 '12 at 17:04













  • nice tool thx!!

    – DannyRe
    Jul 27 '12 at 17:13











  • Indeed. And for arbitrary fields, ps -o <field> $(pgrep mysql).

    – Mikel
    Jul 27 '12 at 17:22






  • 1





    or ps h -o %p -C mysqld if you want the PIDs of a particular named process (-C is exact match, not search pattern or regexp). You can have multiple -C args, e.g. ps h -o %p -C mysqld -C mysql to get client and server processes.

    – cas
    Jul 27 '12 at 21:06














2












2








2








I want to use my awk shortcut as a function, so that I can pass the column number which then prints me the output. My aliases are:



alias A="| awk '{print $1}'
alias G="| grep -i'


Instad of typing:



ps -ef | grep mysql | awk 'print $2'


I want to be able to type this:



ps -ef G mysql A 2


Any suggestions?










share|improve this question
















I want to use my awk shortcut as a function, so that I can pass the column number which then prints me the output. My aliases are:



alias A="| awk '{print $1}'
alias G="| grep -i'


Instad of typing:



ps -ef | grep mysql | awk 'print $2'


I want to be able to type this:



ps -ef G mysql A 2


Any suggestions?







shell alias function






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 23 '16 at 13:14









John Militer

6592928




6592928










asked Jul 27 '12 at 16:52









DannyReDannyRe

1114




1114








  • 4





    For a completely different approach, try pgrep -f mysql (assuming pgrep is available).

    – jw013
    Jul 27 '12 at 17:04













  • nice tool thx!!

    – DannyRe
    Jul 27 '12 at 17:13











  • Indeed. And for arbitrary fields, ps -o <field> $(pgrep mysql).

    – Mikel
    Jul 27 '12 at 17:22






  • 1





    or ps h -o %p -C mysqld if you want the PIDs of a particular named process (-C is exact match, not search pattern or regexp). You can have multiple -C args, e.g. ps h -o %p -C mysqld -C mysql to get client and server processes.

    – cas
    Jul 27 '12 at 21:06














  • 4





    For a completely different approach, try pgrep -f mysql (assuming pgrep is available).

    – jw013
    Jul 27 '12 at 17:04













  • nice tool thx!!

    – DannyRe
    Jul 27 '12 at 17:13











  • Indeed. And for arbitrary fields, ps -o <field> $(pgrep mysql).

    – Mikel
    Jul 27 '12 at 17:22






  • 1





    or ps h -o %p -C mysqld if you want the PIDs of a particular named process (-C is exact match, not search pattern or regexp). You can have multiple -C args, e.g. ps h -o %p -C mysqld -C mysql to get client and server processes.

    – cas
    Jul 27 '12 at 21:06








4




4





For a completely different approach, try pgrep -f mysql (assuming pgrep is available).

– jw013
Jul 27 '12 at 17:04







For a completely different approach, try pgrep -f mysql (assuming pgrep is available).

– jw013
Jul 27 '12 at 17:04















nice tool thx!!

– DannyRe
Jul 27 '12 at 17:13





nice tool thx!!

– DannyRe
Jul 27 '12 at 17:13













Indeed. And for arbitrary fields, ps -o <field> $(pgrep mysql).

– Mikel
Jul 27 '12 at 17:22





Indeed. And for arbitrary fields, ps -o <field> $(pgrep mysql).

– Mikel
Jul 27 '12 at 17:22




1




1





or ps h -o %p -C mysqld if you want the PIDs of a particular named process (-C is exact match, not search pattern or regexp). You can have multiple -C args, e.g. ps h -o %p -C mysqld -C mysql to get client and server processes.

– cas
Jul 27 '12 at 21:06





or ps h -o %p -C mysqld if you want the PIDs of a particular named process (-C is exact match, not search pattern or regexp). You can have multiple -C args, e.g. ps h -o %p -C mysqld -C mysql to get client and server processes.

– cas
Jul 27 '12 at 21:06










2 Answers
2






active

oldest

votes


















4














I don't think it's possible. Basically, aliases can't take arguments ($1), and functions can't do macro expansion (|).



The closest options I can think of:



in bash or zsh



C() { col=$1; shift; eval "awkcmd='{ print $$col }'"; echo "$awkcmd"; "$@" | awk "$awkcmd"; }

C 2 ps -ef G mysql


in zsh



alias -g F="| tr -s '[[:space:]]' | cut -d ' ' -f"

ps -ef G mysql F 2





share|improve this answer































    0














    Putting pipes into aliases is awkward. Instead, create a shell function that you can pipe things to:



    G () {
    grep -i "$@"
    }

    A () {
    awk -v col="$1" '{ print $col }'
    }


    Then,



    ps -ef | G mysql | A 2


    But this particular pipeline would, on a Linux system, be more or less the same as



    pgrep mysql





    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',
      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%2f44135%2fawk-function-with-a-number-parameter-for-the-column-you-want-to-print%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









      4














      I don't think it's possible. Basically, aliases can't take arguments ($1), and functions can't do macro expansion (|).



      The closest options I can think of:



      in bash or zsh



      C() { col=$1; shift; eval "awkcmd='{ print $$col }'"; echo "$awkcmd"; "$@" | awk "$awkcmd"; }

      C 2 ps -ef G mysql


      in zsh



      alias -g F="| tr -s '[[:space:]]' | cut -d ' ' -f"

      ps -ef G mysql F 2





      share|improve this answer




























        4














        I don't think it's possible. Basically, aliases can't take arguments ($1), and functions can't do macro expansion (|).



        The closest options I can think of:



        in bash or zsh



        C() { col=$1; shift; eval "awkcmd='{ print $$col }'"; echo "$awkcmd"; "$@" | awk "$awkcmd"; }

        C 2 ps -ef G mysql


        in zsh



        alias -g F="| tr -s '[[:space:]]' | cut -d ' ' -f"

        ps -ef G mysql F 2





        share|improve this answer


























          4












          4








          4







          I don't think it's possible. Basically, aliases can't take arguments ($1), and functions can't do macro expansion (|).



          The closest options I can think of:



          in bash or zsh



          C() { col=$1; shift; eval "awkcmd='{ print $$col }'"; echo "$awkcmd"; "$@" | awk "$awkcmd"; }

          C 2 ps -ef G mysql


          in zsh



          alias -g F="| tr -s '[[:space:]]' | cut -d ' ' -f"

          ps -ef G mysql F 2





          share|improve this answer













          I don't think it's possible. Basically, aliases can't take arguments ($1), and functions can't do macro expansion (|).



          The closest options I can think of:



          in bash or zsh



          C() { col=$1; shift; eval "awkcmd='{ print $$col }'"; echo "$awkcmd"; "$@" | awk "$awkcmd"; }

          C 2 ps -ef G mysql


          in zsh



          alias -g F="| tr -s '[[:space:]]' | cut -d ' ' -f"

          ps -ef G mysql F 2






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 27 '12 at 17:10









          MikelMikel

          40k10103127




          40k10103127

























              0














              Putting pipes into aliases is awkward. Instead, create a shell function that you can pipe things to:



              G () {
              grep -i "$@"
              }

              A () {
              awk -v col="$1" '{ print $col }'
              }


              Then,



              ps -ef | G mysql | A 2


              But this particular pipeline would, on a Linux system, be more or less the same as



              pgrep mysql





              share|improve this answer




























                0














                Putting pipes into aliases is awkward. Instead, create a shell function that you can pipe things to:



                G () {
                grep -i "$@"
                }

                A () {
                awk -v col="$1" '{ print $col }'
                }


                Then,



                ps -ef | G mysql | A 2


                But this particular pipeline would, on a Linux system, be more or less the same as



                pgrep mysql





                share|improve this answer


























                  0












                  0








                  0







                  Putting pipes into aliases is awkward. Instead, create a shell function that you can pipe things to:



                  G () {
                  grep -i "$@"
                  }

                  A () {
                  awk -v col="$1" '{ print $col }'
                  }


                  Then,



                  ps -ef | G mysql | A 2


                  But this particular pipeline would, on a Linux system, be more or less the same as



                  pgrep mysql





                  share|improve this answer













                  Putting pipes into aliases is awkward. Instead, create a shell function that you can pipe things to:



                  G () {
                  grep -i "$@"
                  }

                  A () {
                  awk -v col="$1" '{ print $col }'
                  }


                  Then,



                  ps -ef | G mysql | A 2


                  But this particular pipeline would, on a Linux system, be more or less the same as



                  pgrep mysql






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 28 at 20:57









                  KusalanandaKusalananda

                  137k17258426




                  137k17258426






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f44135%2fawk-function-with-a-number-parameter-for-the-column-you-want-to-print%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

                      How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

                      is 'sed' thread safe

                      How to make a Squid Proxy server?