Creating new columns calculating the value in the same row in Linux

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have a table:



 A B C
X 1 2 3
Y 4 5 6
Z 7 8 9


I want to create two new columns D and E, calculating the average and the value of a formula (A+B)/C respectively to get:



 A B C D E
X 1 2 3 2 1
Y 4 5 6 5 1.5
Z 7 8 9 8 1.67


How to do that? All post I found from search are calculating values in a column but not row, and output to another file.







share|improve this question






















  • Are the ABCXYZ labels actually part of your file? If so, you'd be better off removing them and just having the data itself in the file.
    – Wildcard
    Jan 23 at 3:50














up vote
1
down vote

favorite












I have a table:



 A B C
X 1 2 3
Y 4 5 6
Z 7 8 9


I want to create two new columns D and E, calculating the average and the value of a formula (A+B)/C respectively to get:



 A B C D E
X 1 2 3 2 1
Y 4 5 6 5 1.5
Z 7 8 9 8 1.67


How to do that? All post I found from search are calculating values in a column but not row, and output to another file.







share|improve this question






















  • Are the ABCXYZ labels actually part of your file? If so, you'd be better off removing them and just having the data itself in the file.
    – Wildcard
    Jan 23 at 3:50












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a table:



 A B C
X 1 2 3
Y 4 5 6
Z 7 8 9


I want to create two new columns D and E, calculating the average and the value of a formula (A+B)/C respectively to get:



 A B C D E
X 1 2 3 2 1
Y 4 5 6 5 1.5
Z 7 8 9 8 1.67


How to do that? All post I found from search are calculating values in a column but not row, and output to another file.







share|improve this question














I have a table:



 A B C
X 1 2 3
Y 4 5 6
Z 7 8 9


I want to create two new columns D and E, calculating the average and the value of a formula (A+B)/C respectively to get:



 A B C D E
X 1 2 3 2 1
Y 4 5 6 5 1.5
Z 7 8 9 8 1.67


How to do that? All post I found from search are calculating values in a column but not row, and output to another file.









share|improve this question













share|improve this question




share|improve this question








edited Jan 23 at 7:25









αғsнιη

15.2k92462




15.2k92462










asked Jan 23 at 3:35









Johnny Tam

1208




1208











  • Are the ABCXYZ labels actually part of your file? If so, you'd be better off removing them and just having the data itself in the file.
    – Wildcard
    Jan 23 at 3:50
















  • Are the ABCXYZ labels actually part of your file? If so, you'd be better off removing them and just having the data itself in the file.
    – Wildcard
    Jan 23 at 3:50















Are the ABCXYZ labels actually part of your file? If so, you'd be better off removing them and just having the data itself in the file.
– Wildcard
Jan 23 at 3:50




Are the ABCXYZ labels actually part of your file? If so, you'd be better off removing them and just having the data itself in the file.
– Wildcard
Jan 23 at 3:50










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Given data.txt containing:



1 2 3
4 5 6
7 8 9


Run:



awk '$4 = ($1+$2+$3)/3; $5 = ($1+$2)/$3; print' data.txt


Output will be:



1 2 3 2 1
4 5 6 5 1.5
7 8 9 8 1.66667





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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f418996%2fcreating-new-columns-calculating-the-value-in-the-same-row-in-linux%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    Given data.txt containing:



    1 2 3
    4 5 6
    7 8 9


    Run:



    awk '$4 = ($1+$2+$3)/3; $5 = ($1+$2)/$3; print' data.txt


    Output will be:



    1 2 3 2 1
    4 5 6 5 1.5
    7 8 9 8 1.66667





    share|improve this answer
























      up vote
      2
      down vote



      accepted










      Given data.txt containing:



      1 2 3
      4 5 6
      7 8 9


      Run:



      awk '$4 = ($1+$2+$3)/3; $5 = ($1+$2)/$3; print' data.txt


      Output will be:



      1 2 3 2 1
      4 5 6 5 1.5
      7 8 9 8 1.66667





      share|improve this answer






















        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        Given data.txt containing:



        1 2 3
        4 5 6
        7 8 9


        Run:



        awk '$4 = ($1+$2+$3)/3; $5 = ($1+$2)/$3; print' data.txt


        Output will be:



        1 2 3 2 1
        4 5 6 5 1.5
        7 8 9 8 1.66667





        share|improve this answer












        Given data.txt containing:



        1 2 3
        4 5 6
        7 8 9


        Run:



        awk '$4 = ($1+$2+$3)/3; $5 = ($1+$2)/$3; print' data.txt


        Output will be:



        1 2 3 2 1
        4 5 6 5 1.5
        7 8 9 8 1.66667






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 23 at 3:52









        Wildcard

        22k855154




        22k855154






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f418996%2fcreating-new-columns-calculating-the-value-in-the-same-row-in-linux%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay