Aliased pipeline using head and cut












4















I'd like to create an alias to have a quick view of the table format files with comma separator:



alias thead='head | cut -d, -f1- | column -s, -t'


Later using it like this



thead file.csv


However, it doesn't work. What would be the correct syntax?










share|improve this question





























    4















    I'd like to create an alias to have a quick view of the table format files with comma separator:



    alias thead='head | cut -d, -f1- | column -s, -t'


    Later using it like this



    thead file.csv


    However, it doesn't work. What would be the correct syntax?










    share|improve this question



























      4












      4








      4


      1






      I'd like to create an alias to have a quick view of the table format files with comma separator:



      alias thead='head | cut -d, -f1- | column -s, -t'


      Later using it like this



      thead file.csv


      However, it doesn't work. What would be the correct syntax?










      share|improve this question
















      I'd like to create an alias to have a quick view of the table format files with comma separator:



      alias thead='head | cut -d, -f1- | column -s, -t'


      Later using it like this



      thead file.csv


      However, it doesn't work. What would be the correct syntax?







      bash alias






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 21 at 17:29









      Kusalananda

      137k17258426




      137k17258426










      asked Feb 21 at 16:51









      Max LiMax Li

      1234




      1234






















          2 Answers
          2






          active

          oldest

          votes


















          9














          For anything more advanced than a simple command, use a shell function instead of an alias:



          thead () {
          head "$1" | cut -d, -f1- | column -s, -t
          }


          This shell function would run head on its first argument, and then send the result through the pipeline (although, since the cut gets all columns due to -f 1-, this part can probably be removed; I'm leaving it in here as you had it in your original pipeline).



          Or,



          thead () {
          head "$2" | cut -d "$1" -f1- | column -s "$1" -t
          }


          ... to be able to use it as



          thead ',' filename


          Or even, to allow for an optional delimiter (and use comma if none is given),



          thead () {
          local delim=','

          if [ "$#" -gt 1 ]; then
          delim=$1
          shift
          fi

          head "$1" | cut -d "$delim" -f1- | column -s "$delim" -t
          }


          The function definition above could be placed wherever you usually define aliases.





          The issue with having a pipeline in an alias is that when you use the alias with an argument, this argument would be added to the end of the pipeline, not after the first command in the pipeline.





          The bash manual contains the sentence




          For almost every purpose, aliases are superseded by shell functions.







          share|improve this answer


























          • that's good, do you know btw how can I adjust it to accept a separator as a parameter to thead?

            – Max Li
            Feb 21 at 16:59











          • @MaxLi See updated answer.

            – Kusalananda
            Feb 21 at 17:01











          • @MaxLi I noticed that the delimiter was also present in the column command and that your cut command is a no-op. I fixed delimiter for column and added a note about the cut.

            – Kusalananda
            Feb 21 at 18:55



















          9














          alias expansion is just text substitution which is parsed again by the shell, so when you do:



          thead file.csv


          That's just replaced with:



          head | cut -d, -f1- | column -s, -t file.csv


          and interpreted again.



          If you had written:



          <file.csv thead


          or



          cat file.csv | thead


          or



          { thead; } < file.csv


          It would have worked as it would have been replaced with:



          <file.csv head | cut -d, -f1- | column -s, -t
          cat file.csv | head | cut -d, -f1- | column -s, -t
          { head | cut -d, -f1- | column -s, -t; } < file.csv


          respectively. In any case, as @Kusalananda says, it's much better to use functions or scripts than aliases for that. Here, I'd just do:



          thead() { head "$@" | cut -d, -f1- | column -s, -t; }


          So you can do thead -n 12 file.csv file2.csv for instance.






          share|improve this answer
























          • I think this is the better answer as it clearly explains why the OP’s attempt didn’t work.

            – comfreak
            Feb 21 at 21:14











          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%2f502125%2faliased-pipeline-using-head-and-cut%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









          9














          For anything more advanced than a simple command, use a shell function instead of an alias:



          thead () {
          head "$1" | cut -d, -f1- | column -s, -t
          }


          This shell function would run head on its first argument, and then send the result through the pipeline (although, since the cut gets all columns due to -f 1-, this part can probably be removed; I'm leaving it in here as you had it in your original pipeline).



          Or,



          thead () {
          head "$2" | cut -d "$1" -f1- | column -s "$1" -t
          }


          ... to be able to use it as



          thead ',' filename


          Or even, to allow for an optional delimiter (and use comma if none is given),



          thead () {
          local delim=','

          if [ "$#" -gt 1 ]; then
          delim=$1
          shift
          fi

          head "$1" | cut -d "$delim" -f1- | column -s "$delim" -t
          }


          The function definition above could be placed wherever you usually define aliases.





          The issue with having a pipeline in an alias is that when you use the alias with an argument, this argument would be added to the end of the pipeline, not after the first command in the pipeline.





          The bash manual contains the sentence




          For almost every purpose, aliases are superseded by shell functions.







          share|improve this answer


























          • that's good, do you know btw how can I adjust it to accept a separator as a parameter to thead?

            – Max Li
            Feb 21 at 16:59











          • @MaxLi See updated answer.

            – Kusalananda
            Feb 21 at 17:01











          • @MaxLi I noticed that the delimiter was also present in the column command and that your cut command is a no-op. I fixed delimiter for column and added a note about the cut.

            – Kusalananda
            Feb 21 at 18:55
















          9














          For anything more advanced than a simple command, use a shell function instead of an alias:



          thead () {
          head "$1" | cut -d, -f1- | column -s, -t
          }


          This shell function would run head on its first argument, and then send the result through the pipeline (although, since the cut gets all columns due to -f 1-, this part can probably be removed; I'm leaving it in here as you had it in your original pipeline).



          Or,



          thead () {
          head "$2" | cut -d "$1" -f1- | column -s "$1" -t
          }


          ... to be able to use it as



          thead ',' filename


          Or even, to allow for an optional delimiter (and use comma if none is given),



          thead () {
          local delim=','

          if [ "$#" -gt 1 ]; then
          delim=$1
          shift
          fi

          head "$1" | cut -d "$delim" -f1- | column -s "$delim" -t
          }


          The function definition above could be placed wherever you usually define aliases.





          The issue with having a pipeline in an alias is that when you use the alias with an argument, this argument would be added to the end of the pipeline, not after the first command in the pipeline.





          The bash manual contains the sentence




          For almost every purpose, aliases are superseded by shell functions.







          share|improve this answer


























          • that's good, do you know btw how can I adjust it to accept a separator as a parameter to thead?

            – Max Li
            Feb 21 at 16:59











          • @MaxLi See updated answer.

            – Kusalananda
            Feb 21 at 17:01











          • @MaxLi I noticed that the delimiter was also present in the column command and that your cut command is a no-op. I fixed delimiter for column and added a note about the cut.

            – Kusalananda
            Feb 21 at 18:55














          9












          9








          9







          For anything more advanced than a simple command, use a shell function instead of an alias:



          thead () {
          head "$1" | cut -d, -f1- | column -s, -t
          }


          This shell function would run head on its first argument, and then send the result through the pipeline (although, since the cut gets all columns due to -f 1-, this part can probably be removed; I'm leaving it in here as you had it in your original pipeline).



          Or,



          thead () {
          head "$2" | cut -d "$1" -f1- | column -s "$1" -t
          }


          ... to be able to use it as



          thead ',' filename


          Or even, to allow for an optional delimiter (and use comma if none is given),



          thead () {
          local delim=','

          if [ "$#" -gt 1 ]; then
          delim=$1
          shift
          fi

          head "$1" | cut -d "$delim" -f1- | column -s "$delim" -t
          }


          The function definition above could be placed wherever you usually define aliases.





          The issue with having a pipeline in an alias is that when you use the alias with an argument, this argument would be added to the end of the pipeline, not after the first command in the pipeline.





          The bash manual contains the sentence




          For almost every purpose, aliases are superseded by shell functions.







          share|improve this answer















          For anything more advanced than a simple command, use a shell function instead of an alias:



          thead () {
          head "$1" | cut -d, -f1- | column -s, -t
          }


          This shell function would run head on its first argument, and then send the result through the pipeline (although, since the cut gets all columns due to -f 1-, this part can probably be removed; I'm leaving it in here as you had it in your original pipeline).



          Or,



          thead () {
          head "$2" | cut -d "$1" -f1- | column -s "$1" -t
          }


          ... to be able to use it as



          thead ',' filename


          Or even, to allow for an optional delimiter (and use comma if none is given),



          thead () {
          local delim=','

          if [ "$#" -gt 1 ]; then
          delim=$1
          shift
          fi

          head "$1" | cut -d "$delim" -f1- | column -s "$delim" -t
          }


          The function definition above could be placed wherever you usually define aliases.





          The issue with having a pipeline in an alias is that when you use the alias with an argument, this argument would be added to the end of the pipeline, not after the first command in the pipeline.





          The bash manual contains the sentence




          For almost every purpose, aliases are superseded by shell functions.








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 21 at 18:51

























          answered Feb 21 at 16:54









          KusalanandaKusalananda

          137k17258426




          137k17258426













          • that's good, do you know btw how can I adjust it to accept a separator as a parameter to thead?

            – Max Li
            Feb 21 at 16:59











          • @MaxLi See updated answer.

            – Kusalananda
            Feb 21 at 17:01











          • @MaxLi I noticed that the delimiter was also present in the column command and that your cut command is a no-op. I fixed delimiter for column and added a note about the cut.

            – Kusalananda
            Feb 21 at 18:55



















          • that's good, do you know btw how can I adjust it to accept a separator as a parameter to thead?

            – Max Li
            Feb 21 at 16:59











          • @MaxLi See updated answer.

            – Kusalananda
            Feb 21 at 17:01











          • @MaxLi I noticed that the delimiter was also present in the column command and that your cut command is a no-op. I fixed delimiter for column and added a note about the cut.

            – Kusalananda
            Feb 21 at 18:55

















          that's good, do you know btw how can I adjust it to accept a separator as a parameter to thead?

          – Max Li
          Feb 21 at 16:59





          that's good, do you know btw how can I adjust it to accept a separator as a parameter to thead?

          – Max Li
          Feb 21 at 16:59













          @MaxLi See updated answer.

          – Kusalananda
          Feb 21 at 17:01





          @MaxLi See updated answer.

          – Kusalananda
          Feb 21 at 17:01













          @MaxLi I noticed that the delimiter was also present in the column command and that your cut command is a no-op. I fixed delimiter for column and added a note about the cut.

          – Kusalananda
          Feb 21 at 18:55





          @MaxLi I noticed that the delimiter was also present in the column command and that your cut command is a no-op. I fixed delimiter for column and added a note about the cut.

          – Kusalananda
          Feb 21 at 18:55













          9














          alias expansion is just text substitution which is parsed again by the shell, so when you do:



          thead file.csv


          That's just replaced with:



          head | cut -d, -f1- | column -s, -t file.csv


          and interpreted again.



          If you had written:



          <file.csv thead


          or



          cat file.csv | thead


          or



          { thead; } < file.csv


          It would have worked as it would have been replaced with:



          <file.csv head | cut -d, -f1- | column -s, -t
          cat file.csv | head | cut -d, -f1- | column -s, -t
          { head | cut -d, -f1- | column -s, -t; } < file.csv


          respectively. In any case, as @Kusalananda says, it's much better to use functions or scripts than aliases for that. Here, I'd just do:



          thead() { head "$@" | cut -d, -f1- | column -s, -t; }


          So you can do thead -n 12 file.csv file2.csv for instance.






          share|improve this answer
























          • I think this is the better answer as it clearly explains why the OP’s attempt didn’t work.

            – comfreak
            Feb 21 at 21:14
















          9














          alias expansion is just text substitution which is parsed again by the shell, so when you do:



          thead file.csv


          That's just replaced with:



          head | cut -d, -f1- | column -s, -t file.csv


          and interpreted again.



          If you had written:



          <file.csv thead


          or



          cat file.csv | thead


          or



          { thead; } < file.csv


          It would have worked as it would have been replaced with:



          <file.csv head | cut -d, -f1- | column -s, -t
          cat file.csv | head | cut -d, -f1- | column -s, -t
          { head | cut -d, -f1- | column -s, -t; } < file.csv


          respectively. In any case, as @Kusalananda says, it's much better to use functions or scripts than aliases for that. Here, I'd just do:



          thead() { head "$@" | cut -d, -f1- | column -s, -t; }


          So you can do thead -n 12 file.csv file2.csv for instance.






          share|improve this answer
























          • I think this is the better answer as it clearly explains why the OP’s attempt didn’t work.

            – comfreak
            Feb 21 at 21:14














          9












          9








          9







          alias expansion is just text substitution which is parsed again by the shell, so when you do:



          thead file.csv


          That's just replaced with:



          head | cut -d, -f1- | column -s, -t file.csv


          and interpreted again.



          If you had written:



          <file.csv thead


          or



          cat file.csv | thead


          or



          { thead; } < file.csv


          It would have worked as it would have been replaced with:



          <file.csv head | cut -d, -f1- | column -s, -t
          cat file.csv | head | cut -d, -f1- | column -s, -t
          { head | cut -d, -f1- | column -s, -t; } < file.csv


          respectively. In any case, as @Kusalananda says, it's much better to use functions or scripts than aliases for that. Here, I'd just do:



          thead() { head "$@" | cut -d, -f1- | column -s, -t; }


          So you can do thead -n 12 file.csv file2.csv for instance.






          share|improve this answer













          alias expansion is just text substitution which is parsed again by the shell, so when you do:



          thead file.csv


          That's just replaced with:



          head | cut -d, -f1- | column -s, -t file.csv


          and interpreted again.



          If you had written:



          <file.csv thead


          or



          cat file.csv | thead


          or



          { thead; } < file.csv


          It would have worked as it would have been replaced with:



          <file.csv head | cut -d, -f1- | column -s, -t
          cat file.csv | head | cut -d, -f1- | column -s, -t
          { head | cut -d, -f1- | column -s, -t; } < file.csv


          respectively. In any case, as @Kusalananda says, it's much better to use functions or scripts than aliases for that. Here, I'd just do:



          thead() { head "$@" | cut -d, -f1- | column -s, -t; }


          So you can do thead -n 12 file.csv file2.csv for instance.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 21 at 17:31









          Stéphane ChazelasStéphane Chazelas

          311k57587945




          311k57587945













          • I think this is the better answer as it clearly explains why the OP’s attempt didn’t work.

            – comfreak
            Feb 21 at 21:14



















          • I think this is the better answer as it clearly explains why the OP’s attempt didn’t work.

            – comfreak
            Feb 21 at 21:14

















          I think this is the better answer as it clearly explains why the OP’s attempt didn’t work.

          – comfreak
          Feb 21 at 21:14





          I think this is the better answer as it clearly explains why the OP’s attempt didn’t work.

          – comfreak
          Feb 21 at 21:14


















          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%2f502125%2faliased-pipeline-using-head-and-cut%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 do I know what Microsoft account the skydrive app is syncing to?

          When does type information flow backwards in C++?

          Grease: Live!