Aliased pipeline using head and cutHow to use ' in alias?Bash newline doesn't printTail multiple files and...

Translation for threshold (figuratively)

How do I know my password or backup information is not being shared when creating a new wallet?

Is there a way to pause a running process on Linux systems and resume later?

Isn't a semicolon (';') needed after a function declaration in C++?

Use intersection in field calculator

How do I write a maintainable, fast, compile-time bit-mask in C++?

Using font-relative distances in tikzpictures

What is the difference between crontab -e and nano /etc/crontab?

Boss asked me to sign a resignation paper without a date on it along with my new contract

What did Putin say about a US deep state in his state-of-the-nation speech; what has he said in the past?

Will linear voltage regulator step up current?

What is the reason behind this musical reference to Pinocchio in the Close Encounters main theme?

Multiple null checks in Java 8

Why didn't Lorentz conclude that no object can go faster than light?

Why do we divide Permutations to get to Combinations?

Is layered encryption more secure than long passwords?

The Longest Chess Game

Is it common to refer to someone as "Prof. Dr. [LastName]"?

What does an unprocessed RAW file look like?

TikZtree with asymmetric siblings

Aliased pipeline using head and cut

Are encryption algorithms with fixed-point free permutations inherently flawed?

Have the UK Conservatives lost the working majority and if so, what does this mean?

Is it ethical to apply for a job on someone's behalf?



Aliased pipeline using head and cut


How to use ' in alias?Bash newline doesn't printTail multiple files and output as additional column with 'find' resultsHow to document my custom bash functions and aliases?aliasing a slightly complex script on Linux/bashHow to escape single quotes correctly creating an aliasWhy doesn't this tee with process substitution produce the 1st and chosen lines?zsh alias with linefeeds, commas and quotesUsing a bash alias or function with environment variables on multiple linesSwapping STD{OUT,ERR} in a pipeline multiple times













3















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









New contributor




Max Li is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    something is missing after the "-d"

    – Rui F Ribeiro
    1 hour ago











  • @RuiFRibeiro yep, a typo, thx

    – Max Li
    1 hour ago
















3















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









New contributor




Max Li is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    something is missing after the "-d"

    – Rui F Ribeiro
    1 hour ago











  • @RuiFRibeiro yep, a typo, thx

    – Max Li
    1 hour ago














3












3








3








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









New contributor




Max Li is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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









New contributor




Max Li is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Max Li is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 44 mins ago









Kusalananda

131k17250409




131k17250409






New contributor




Max Li is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 1 hour ago









Max LiMax Li

1184




1184




New contributor




Max Li is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Max Li is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Max Li is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1





    something is missing after the "-d"

    – Rui F Ribeiro
    1 hour ago











  • @RuiFRibeiro yep, a typo, thx

    – Max Li
    1 hour ago














  • 1





    something is missing after the "-d"

    – Rui F Ribeiro
    1 hour ago











  • @RuiFRibeiro yep, a typo, thx

    – Max Li
    1 hour ago








1




1





something is missing after the "-d"

– Rui F Ribeiro
1 hour ago





something is missing after the "-d"

– Rui F Ribeiro
1 hour ago













@RuiFRibeiro yep, a typo, thx

– Max Li
1 hour ago





@RuiFRibeiro yep, a typo, thx

– Max Li
1 hour ago










2 Answers
2






active

oldest

votes


















4














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.



Or,



thead () {
head "$2" | cut -d "$1" -f1- | column -s, -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, -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
    1 hour ago











  • @MaxLi See updated answer.

    – Kusalananda
    1 hour ago



















2














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























    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
    });


    }
    });






    Max Li is a new contributor. Be nice, and check out our Code of Conduct.










    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









    4














    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.



    Or,



    thead () {
    head "$2" | cut -d "$1" -f1- | column -s, -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, -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
      1 hour ago











    • @MaxLi See updated answer.

      – Kusalananda
      1 hour ago
















    4














    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.



    Or,



    thead () {
    head "$2" | cut -d "$1" -f1- | column -s, -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, -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
      1 hour ago











    • @MaxLi See updated answer.

      – Kusalananda
      1 hour ago














    4












    4








    4







    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.



    Or,



    thead () {
    head "$2" | cut -d "$1" -f1- | column -s, -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, -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.



    Or,



    thead () {
    head "$2" | cut -d "$1" -f1- | column -s, -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, -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 23 mins ago

























    answered 1 hour ago









    KusalanandaKusalananda

    131k17250409




    131k17250409













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

      – Max Li
      1 hour ago











    • @MaxLi See updated answer.

      – Kusalananda
      1 hour ago



















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

      – Max Li
      1 hour ago











    • @MaxLi See updated answer.

      – Kusalananda
      1 hour ago

















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

    – Max Li
    1 hour ago





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

    – Max Li
    1 hour ago













    @MaxLi See updated answer.

    – Kusalananda
    1 hour ago





    @MaxLi See updated answer.

    – Kusalananda
    1 hour ago













    2














    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




























      2














      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


























        2












        2








        2







        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 41 mins ago









        Stéphane ChazelasStéphane Chazelas

        306k57580935




        306k57580935






















            Max Li is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Max Li is a new contributor. Be nice, and check out our Code of Conduct.













            Max Li is a new contributor. Be nice, and check out our Code of Conduct.












            Max Li is a new contributor. Be nice, and check out our Code of Conduct.
















            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

            Anexo:Material bélico de la Fuerza Aérea de Chile Índice Aeronaves Defensa...

            Always On Availability groups resolving state after failover - Remote harden of transaction...

            update json value to null Announcing the arrival of Valued Associate #679: Cesar Manara ...