Value addition from a file's rowNeed to sum of last column based on pipe delimiterRecover files from corrupt...

Create less file for custom Theme

Is there an application which does HTTP PUT?

Is there a need for better software for writers?

My perfect evil overlord plan... or is it?

Was the Highlands Ranch shooting the 115th mass shooting in the US in 2019

Removing all characters except digits from clipboard

Is it a Munchausen Number?

Company stopped paying my salary. What are my options?

How are one-time password generators like Google Authenticator different from having two passwords?

Has there been evidence of any other gods?

Why do Thanos' punches not kill Captain America or at least cause some mortal injuries?

What is the name of meteoroids which hit Moon, Mars, or pretty much anything that isn’t the Earth?

Noob at soldering, can anyone explain why my circuit wont work?

A Cunning Riley Riddle

Why does increasing the sampling rate make implementing an anti-aliasing filter easier?

Further factorisation of a difference of cubes?

How to select certain lines (n, n+4, n+8, n+12...) from the file?

What was the notion of limit that Newton used?

Windows OS quantum vs. SQL OS Quantum

Why is PerfectForwardSecrecy considered OK, when it has same defects as salt-less password hashing?

Is it bad writing or bad story telling if first person narrative contains more information than the narrator knows?

Why does the Earth follow an elliptical trajectory rather than a parabolic one?

No such column 'DeveloperName' on entity 'RecordType' after Summer '19 release on sandbox

Is every story set in the future "science fiction"?



Value addition from a file's row


Need to sum of last column based on pipe delimiterRecover files from corrupt DVDHow can I receive top-like CPU statistics from the shell?How do I expect 'echo -e “? c”'?Displaying “No Results” When AWK Command has no outputApply a filter to the value of a variable using backticksHow to call script which is having menus, from another scriptBash function assign value to passed parameterShell script to find and replace value from csv fileRounding a number in awkLink variables between two text files






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















Suppose I have one file like:



apple  | red |   2
apple | green | 3
orange | yellow | 3
apple | yellow | 1


Now I need following output as (irrespective of color value should get third number summed):



apple | red | 6
orange | yellow | 3


I tried something like below:



tail -n 4 $firstfile| while IFS=, read -r f2col1 f2col2
do
match1=$(echo $f2col2)
fruit1=$(echo $f2col1)
if [ "${fruit1}" == "${fruit1}" ]; then
match3=`expr ${match3} + ${match1}`


which doesn't seem to be right after second addition.



Could somebody give me better idea?










share|improve this question




















  • 1





    Possible duplicate of Need to sum of last column based on pipe delimiter

    – αғsнιη
    1 hour ago


















0















Suppose I have one file like:



apple  | red |   2
apple | green | 3
orange | yellow | 3
apple | yellow | 1


Now I need following output as (irrespective of color value should get third number summed):



apple | red | 6
orange | yellow | 3


I tried something like below:



tail -n 4 $firstfile| while IFS=, read -r f2col1 f2col2
do
match1=$(echo $f2col2)
fruit1=$(echo $f2col1)
if [ "${fruit1}" == "${fruit1}" ]; then
match3=`expr ${match3} + ${match1}`


which doesn't seem to be right after second addition.



Could somebody give me better idea?










share|improve this question




















  • 1





    Possible duplicate of Need to sum of last column based on pipe delimiter

    – αғsнιη
    1 hour ago














0












0








0








Suppose I have one file like:



apple  | red |   2
apple | green | 3
orange | yellow | 3
apple | yellow | 1


Now I need following output as (irrespective of color value should get third number summed):



apple | red | 6
orange | yellow | 3


I tried something like below:



tail -n 4 $firstfile| while IFS=, read -r f2col1 f2col2
do
match1=$(echo $f2col2)
fruit1=$(echo $f2col1)
if [ "${fruit1}" == "${fruit1}" ]; then
match3=`expr ${match3} + ${match1}`


which doesn't seem to be right after second addition.



Could somebody give me better idea?










share|improve this question
















Suppose I have one file like:



apple  | red |   2
apple | green | 3
orange | yellow | 3
apple | yellow | 1


Now I need following output as (irrespective of color value should get third number summed):



apple | red | 6
orange | yellow | 3


I tried something like below:



tail -n 4 $firstfile| while IFS=, read -r f2col1 f2col2
do
match1=$(echo $f2col2)
fruit1=$(echo $f2col1)
if [ "${fruit1}" == "${fruit1}" ]; then
match3=`expr ${match3} + ${match1}`


which doesn't seem to be right after second addition.



Could somebody give me better idea?







linux shell-script scripting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 4 mins ago







rawal

















asked 2 hours ago









rawalrawal

12




12








  • 1





    Possible duplicate of Need to sum of last column based on pipe delimiter

    – αғsнιη
    1 hour ago














  • 1





    Possible duplicate of Need to sum of last column based on pipe delimiter

    – αғsнιη
    1 hour ago








1




1





Possible duplicate of Need to sum of last column based on pipe delimiter

– αғsнιη
1 hour ago





Possible duplicate of Need to sum of last column based on pipe delimiter

– αғsнιη
1 hour ago










1 Answer
1






active

oldest

votes


















0














You could use awk for the entire operation



awk '{ arr[$1]+=$3; } END {for ( i in arr ) printf "%s | %dn",i,arr[i];}' 


Here is what each part does





  1. { arr[$1]+=$3; } adds the value to an associative array with the
    fruit name as the key

  2. And in the end {for ( i in arr ) printf "%s|%dn",i,arr[i];} prints each member of the array in the format you need






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%2f518124%2fvalue-addition-from-a-files-row%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You could use awk for the entire operation



    awk '{ arr[$1]+=$3; } END {for ( i in arr ) printf "%s | %dn",i,arr[i];}' 


    Here is what each part does





    1. { arr[$1]+=$3; } adds the value to an associative array with the
      fruit name as the key

    2. And in the end {for ( i in arr ) printf "%s|%dn",i,arr[i];} prints each member of the array in the format you need






    share|improve this answer




























      0














      You could use awk for the entire operation



      awk '{ arr[$1]+=$3; } END {for ( i in arr ) printf "%s | %dn",i,arr[i];}' 


      Here is what each part does





      1. { arr[$1]+=$3; } adds the value to an associative array with the
        fruit name as the key

      2. And in the end {for ( i in arr ) printf "%s|%dn",i,arr[i];} prints each member of the array in the format you need






      share|improve this answer


























        0












        0








        0







        You could use awk for the entire operation



        awk '{ arr[$1]+=$3; } END {for ( i in arr ) printf "%s | %dn",i,arr[i];}' 


        Here is what each part does





        1. { arr[$1]+=$3; } adds the value to an associative array with the
          fruit name as the key

        2. And in the end {for ( i in arr ) printf "%s|%dn",i,arr[i];} prints each member of the array in the format you need






        share|improve this answer













        You could use awk for the entire operation



        awk '{ arr[$1]+=$3; } END {for ( i in arr ) printf "%s | %dn",i,arr[i];}' 


        Here is what each part does





        1. { arr[$1]+=$3; } adds the value to an associative array with the
          fruit name as the key

        2. And in the end {for ( i in arr ) printf "%s|%dn",i,arr[i];} prints each member of the array in the format you need







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 49 mins ago









        amisaxamisax

        1,587515




        1,587515






























            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%2f518124%2fvalue-addition-from-a-files-row%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

            Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

            Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

            Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...