Parsing nethogs tracemode output












0














Parsing nethogs



I am trying to parse the output of nethogs -d 1 -t to the following format:



5.65273 767.912
14.2687 1681.15
19.9011 2309.54


where the first (resp. second) column represents the total number of KB/s sent (resp. received) by my machine. Each line is a measurement taken at intervals of 1 seconds (the -d arg).



My attempt until now



By running



sudo nethogs -d 1 -t


I get the following raw output



Adding local address: 192.168.0.23
Adding local address: fe80::cb1b:6973:f77f:34
Ethernet link detected
Waiting for first packet to arrive (see sourceforge.net bug 1019381)

Refreshing:
unknown TCP/0/0 0 0

Refreshing:
/usr/lib/firefox/firefox/12576/1000 4.07988 543.749
/opt/google/chrome/chrome/2441/1000 1.57285 224.163
unknown TCP/0/0 0 0

Refreshing:
/usr/lib/firefox/firefox/12576/1000 11.9787 1330.22
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0 0

Refreshing:
/usr/lib/firefox/firefox/12576/1000 17.6111 1958.61
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0 0


By running the command



sudo nethogs -d 1 -t 2>&1 | sed '1,5d;/Refreshing:/d;s_t_ _g'


I am able to produce a cleaner output which ressembles



unknown TCP/0/0 0 0

/usr/lib/firefox/firefox/12576/1000 4.07988 543.749
/opt/google/chrome/chrome/2441/1000 1.57285 224.163
unknown TCP/0/0 0 0

/usr/lib/firefox/firefox/12576/1000 11.9787 1330.22
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0 0

/usr/lib/firefox/firefox/12576/1000 17.6111 1958.61
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0 0


Now how to I go from this to my desired output? I tried to use awk to sum the outputs for one measurement like this



sudo nethogs -d 1 -t 2>&1 | sed '1,5d;/Refreshing:/d;s_t_ _g' | awk 'BEGIN{sent=0; recv=0;};{if (NF >= 2) sent+=$(NF-1); recv+=$NF;}; END{print sent, recv;};'


but that does not produce any output. I could put the output of sed in a file and then apply awk to each paragraph but I feel like there is a more direct way to do it and one which would give me continuous output which I would prefer.










share|improve this question
























  • For us that do not have access to nethogs, what does the output that you are parsing look like?
    – Kusalananda
    Dec 16 at 9:03








  • 1




    @Kusalananda done :)
    – Corvinus
    Dec 16 at 9:15
















0














Parsing nethogs



I am trying to parse the output of nethogs -d 1 -t to the following format:



5.65273 767.912
14.2687 1681.15
19.9011 2309.54


where the first (resp. second) column represents the total number of KB/s sent (resp. received) by my machine. Each line is a measurement taken at intervals of 1 seconds (the -d arg).



My attempt until now



By running



sudo nethogs -d 1 -t


I get the following raw output



Adding local address: 192.168.0.23
Adding local address: fe80::cb1b:6973:f77f:34
Ethernet link detected
Waiting for first packet to arrive (see sourceforge.net bug 1019381)

Refreshing:
unknown TCP/0/0 0 0

Refreshing:
/usr/lib/firefox/firefox/12576/1000 4.07988 543.749
/opt/google/chrome/chrome/2441/1000 1.57285 224.163
unknown TCP/0/0 0 0

Refreshing:
/usr/lib/firefox/firefox/12576/1000 11.9787 1330.22
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0 0

Refreshing:
/usr/lib/firefox/firefox/12576/1000 17.6111 1958.61
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0 0


By running the command



sudo nethogs -d 1 -t 2>&1 | sed '1,5d;/Refreshing:/d;s_t_ _g'


I am able to produce a cleaner output which ressembles



unknown TCP/0/0 0 0

/usr/lib/firefox/firefox/12576/1000 4.07988 543.749
/opt/google/chrome/chrome/2441/1000 1.57285 224.163
unknown TCP/0/0 0 0

/usr/lib/firefox/firefox/12576/1000 11.9787 1330.22
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0 0

/usr/lib/firefox/firefox/12576/1000 17.6111 1958.61
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0 0


Now how to I go from this to my desired output? I tried to use awk to sum the outputs for one measurement like this



sudo nethogs -d 1 -t 2>&1 | sed '1,5d;/Refreshing:/d;s_t_ _g' | awk 'BEGIN{sent=0; recv=0;};{if (NF >= 2) sent+=$(NF-1); recv+=$NF;}; END{print sent, recv;};'


but that does not produce any output. I could put the output of sed in a file and then apply awk to each paragraph but I feel like there is a more direct way to do it and one which would give me continuous output which I would prefer.










share|improve this question
























  • For us that do not have access to nethogs, what does the output that you are parsing look like?
    – Kusalananda
    Dec 16 at 9:03








  • 1




    @Kusalananda done :)
    – Corvinus
    Dec 16 at 9:15














0












0








0







Parsing nethogs



I am trying to parse the output of nethogs -d 1 -t to the following format:



5.65273 767.912
14.2687 1681.15
19.9011 2309.54


where the first (resp. second) column represents the total number of KB/s sent (resp. received) by my machine. Each line is a measurement taken at intervals of 1 seconds (the -d arg).



My attempt until now



By running



sudo nethogs -d 1 -t


I get the following raw output



Adding local address: 192.168.0.23
Adding local address: fe80::cb1b:6973:f77f:34
Ethernet link detected
Waiting for first packet to arrive (see sourceforge.net bug 1019381)

Refreshing:
unknown TCP/0/0 0 0

Refreshing:
/usr/lib/firefox/firefox/12576/1000 4.07988 543.749
/opt/google/chrome/chrome/2441/1000 1.57285 224.163
unknown TCP/0/0 0 0

Refreshing:
/usr/lib/firefox/firefox/12576/1000 11.9787 1330.22
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0 0

Refreshing:
/usr/lib/firefox/firefox/12576/1000 17.6111 1958.61
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0 0


By running the command



sudo nethogs -d 1 -t 2>&1 | sed '1,5d;/Refreshing:/d;s_t_ _g'


I am able to produce a cleaner output which ressembles



unknown TCP/0/0 0 0

/usr/lib/firefox/firefox/12576/1000 4.07988 543.749
/opt/google/chrome/chrome/2441/1000 1.57285 224.163
unknown TCP/0/0 0 0

/usr/lib/firefox/firefox/12576/1000 11.9787 1330.22
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0 0

/usr/lib/firefox/firefox/12576/1000 17.6111 1958.61
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0 0


Now how to I go from this to my desired output? I tried to use awk to sum the outputs for one measurement like this



sudo nethogs -d 1 -t 2>&1 | sed '1,5d;/Refreshing:/d;s_t_ _g' | awk 'BEGIN{sent=0; recv=0;};{if (NF >= 2) sent+=$(NF-1); recv+=$NF;}; END{print sent, recv;};'


but that does not produce any output. I could put the output of sed in a file and then apply awk to each paragraph but I feel like there is a more direct way to do it and one which would give me continuous output which I would prefer.










share|improve this question















Parsing nethogs



I am trying to parse the output of nethogs -d 1 -t to the following format:



5.65273 767.912
14.2687 1681.15
19.9011 2309.54


where the first (resp. second) column represents the total number of KB/s sent (resp. received) by my machine. Each line is a measurement taken at intervals of 1 seconds (the -d arg).



My attempt until now



By running



sudo nethogs -d 1 -t


I get the following raw output



Adding local address: 192.168.0.23
Adding local address: fe80::cb1b:6973:f77f:34
Ethernet link detected
Waiting for first packet to arrive (see sourceforge.net bug 1019381)

Refreshing:
unknown TCP/0/0 0 0

Refreshing:
/usr/lib/firefox/firefox/12576/1000 4.07988 543.749
/opt/google/chrome/chrome/2441/1000 1.57285 224.163
unknown TCP/0/0 0 0

Refreshing:
/usr/lib/firefox/firefox/12576/1000 11.9787 1330.22
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0 0

Refreshing:
/usr/lib/firefox/firefox/12576/1000 17.6111 1958.61
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0 0


By running the command



sudo nethogs -d 1 -t 2>&1 | sed '1,5d;/Refreshing:/d;s_t_ _g'


I am able to produce a cleaner output which ressembles



unknown TCP/0/0 0 0

/usr/lib/firefox/firefox/12576/1000 4.07988 543.749
/opt/google/chrome/chrome/2441/1000 1.57285 224.163
unknown TCP/0/0 0 0

/usr/lib/firefox/firefox/12576/1000 11.9787 1330.22
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0 0

/usr/lib/firefox/firefox/12576/1000 17.6111 1958.61
/opt/google/chrome/chrome/2441/1000 2.26895 350.36
unknown TCP/0/0 0 0


Now how to I go from this to my desired output? I tried to use awk to sum the outputs for one measurement like this



sudo nethogs -d 1 -t 2>&1 | sed '1,5d;/Refreshing:/d;s_t_ _g' | awk 'BEGIN{sent=0; recv=0;};{if (NF >= 2) sent+=$(NF-1); recv+=$NF;}; END{print sent, recv;};'


but that does not produce any output. I could put the output of sed in a file and then apply awk to each paragraph but I feel like there is a more direct way to do it and one which would give me continuous output which I would prefer.







shell networking






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 16 at 9:31

























asked Dec 16 at 9:01









Corvinus

32




32












  • For us that do not have access to nethogs, what does the output that you are parsing look like?
    – Kusalananda
    Dec 16 at 9:03








  • 1




    @Kusalananda done :)
    – Corvinus
    Dec 16 at 9:15


















  • For us that do not have access to nethogs, what does the output that you are parsing look like?
    – Kusalananda
    Dec 16 at 9:03








  • 1




    @Kusalananda done :)
    – Corvinus
    Dec 16 at 9:15
















For us that do not have access to nethogs, what does the output that you are parsing look like?
– Kusalananda
Dec 16 at 9:03






For us that do not have access to nethogs, what does the output that you are parsing look like?
– Kusalananda
Dec 16 at 9:03






1




1




@Kusalananda done :)
– Corvinus
Dec 16 at 9:15




@Kusalananda done :)
– Corvinus
Dec 16 at 9:15










1 Answer
1






active

oldest

votes


















0














Just taking the lines that start or don't start with a slash into account:



$ awk '/^[/]/ { sent+=$(NF-1); recv+=$NF }
/^[^/]/ && sent>0 && recv>0 { print sent, recv; sent = recv = 0 }' file
5.65273 767.912
14.2477 1680.58
19.8801 2308.97


This just adds to sent and recv from lines that starts with a slash character. When encountering a line that doesn't start with a slash character, the current accumulated sent and recv values are outputted (and then reset to zero). The output only happens if the variables contain a value greater than zero.






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%2f489278%2fparsing-nethogs-tracemode-output%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














    Just taking the lines that start or don't start with a slash into account:



    $ awk '/^[/]/ { sent+=$(NF-1); recv+=$NF }
    /^[^/]/ && sent>0 && recv>0 { print sent, recv; sent = recv = 0 }' file
    5.65273 767.912
    14.2477 1680.58
    19.8801 2308.97


    This just adds to sent and recv from lines that starts with a slash character. When encountering a line that doesn't start with a slash character, the current accumulated sent and recv values are outputted (and then reset to zero). The output only happens if the variables contain a value greater than zero.






    share|improve this answer


























      0














      Just taking the lines that start or don't start with a slash into account:



      $ awk '/^[/]/ { sent+=$(NF-1); recv+=$NF }
      /^[^/]/ && sent>0 && recv>0 { print sent, recv; sent = recv = 0 }' file
      5.65273 767.912
      14.2477 1680.58
      19.8801 2308.97


      This just adds to sent and recv from lines that starts with a slash character. When encountering a line that doesn't start with a slash character, the current accumulated sent and recv values are outputted (and then reset to zero). The output only happens if the variables contain a value greater than zero.






      share|improve this answer
























        0












        0








        0






        Just taking the lines that start or don't start with a slash into account:



        $ awk '/^[/]/ { sent+=$(NF-1); recv+=$NF }
        /^[^/]/ && sent>0 && recv>0 { print sent, recv; sent = recv = 0 }' file
        5.65273 767.912
        14.2477 1680.58
        19.8801 2308.97


        This just adds to sent and recv from lines that starts with a slash character. When encountering a line that doesn't start with a slash character, the current accumulated sent and recv values are outputted (and then reset to zero). The output only happens if the variables contain a value greater than zero.






        share|improve this answer












        Just taking the lines that start or don't start with a slash into account:



        $ awk '/^[/]/ { sent+=$(NF-1); recv+=$NF }
        /^[^/]/ && sent>0 && recv>0 { print sent, recv; sent = recv = 0 }' file
        5.65273 767.912
        14.2477 1680.58
        19.8801 2308.97


        This just adds to sent and recv from lines that starts with a slash character. When encountering a line that doesn't start with a slash character, the current accumulated sent and recv values are outputted (and then reset to zero). The output only happens if the variables contain a value greater than zero.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 16 at 9:22









        Kusalananda

        121k16229372




        121k16229372






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f489278%2fparsing-nethogs-tracemode-output%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

            Morgemoulin

            Scott Moir

            Souastre