Find the answer to your Linux question:
Results 1 to 3 of 3
output.txt ------------------------------------------------------------ Client connecting to 192.168.1.3, TCP port 5001 TCP window size: 16.0 KByte (default) ------------------------------------------------------------ [ 3] local 192.168.1.4 port 35204 connected with 192.168.1.3 port 5001 [ ID] Interval ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    1

    need a help to extract the content form text file and do some mathemat


    output.txt

    ------------------------------------------------------------
    Client connecting to 192.168.1.3, TCP port 5001
    TCP window size: 16.0 KByte (default)
    ------------------------------------------------------------
    [ 3] local 192.168.1.4 port 35204 connected with 192.168.1.3 port 5001
    [ ID] Interval Transfer Bandwidth
    [ 3] 0.0-10.1 sec 15.2 MBytes 12.7 Mbits/sec
    ------------------------------------------------------------
    Client connecting to 192.168.1.3, TCP port 5001
    TCP window size: 16.0 KByte (default)
    ------------------------------------------------------------
    [ 3] local 192.168.1.4 port 35205 connected with 192.168.1.3 port 5001
    [ ID] Interval Transfer Bandwidth
    [ 3] 0.0-10.1 sec 14.9 MBytes 12.3 Mbits/sec
    ------------------------------------------------------------
    Client connecting to 192.168.1.3, TCP port 5001
    TCP window size: 16.0 KByte (default)
    ------------------------------------------------------------
    [ 3] local 192.168.1.4 port 35206 connected with 192.168.1.3 port 5001
    [ ID] Interval Transfer Bandwidth
    [ 3] 0.0-10.1 sec 14.8 MBytes 12.2 Mbits/sec
    ------------------------------------------------------------



    i need to extract the all the content available below the bandwidth (i.e) 12.2 for every session from that file and i need to perform addition of all the values.

    help me to do that

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    What have you tried? A shell script of grep, awk, and sed (and maybe bc) commands ought to get you there (or perl, which is what I'd use).

  3. #3
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    People often join this forum just to ask the answer to one of their homework questions, then they never come back. So forgive me for not giving an exact answer. But I can tell you the tools you can use

    The "dc" program can perform simple mathematical operations on streams of text. It takes information in "reverse-polish notation", so the key is to convert your "output.txt" file so it looks like this:
    Code:
    #Let's call this file "dc.in"
    0
    16.0 1000 * +
    14.8 1000000 * +
    14.9 1000000 * +
    p
    The first line contains a zero "0", which means, push a "zero" onto the calculator stack. the second line contains "16.0", "1000", "*", and "+". This is code to push "16.0" and "1000" on the stack, then "*" multiplies those two numbers, "then "+" adds the result to the previous number (which was zero). Now the stack has the weighted sum, and the next line is executed. The last line is "p" which means to print the value on the stack.

    Now, how do you convert your "output.txt" to look like "dc.in" as seen above?
    The "sed" program can automatically find patterns in file, and replace those patterns. So you could use the "sed" program like this:
    Code:
    sed -n -e 's,^.*\([0-9.]\+ [kmg]\?bytes\).*$,\1,' \ #search for a number followed by "bytes"
           -e 's,kbytes$, 1000 * +,p' \ #convert the word "kbytes" to the string "1000 * +"
           -e 's,mbytes$, 1000000 * +,p' \ #convert the word "mbytes" to string "1000000 * +"
        output.txt
    
    #FYI: the "-n" tells sed not to print anything until a 'p' pattern-flag is given
    Now, if your wrote your "-e patterns" correctly, sed will have cut out everything but the numbers you need, and replaced KBytes with "1000", MBytes with "100000", etc., and will have also outputted the "*" and "+" operators for "dc". Now, you can use "echo" to print the first and last lines of "dc.in" (the "0" and "p" lines). Wrap the "echo's" and "sed" up in parenthases, and then pipe the output to "dc" like so:
    Code:
    (echo 0; sed -patterns- output.txt; echo p) | dc
    If the output of the parenthases alone looks like the "dc.in" file above, then dc will give you the answer you want.

    So, read up on the "sed" manual, or the "sed" man page, look at some "sed" tutorials to get an idea of how to write your search-replace patterns.

    Sed is tricky to use, so it is important to write your patterns piece by piece, and then test it again and again until it works. Have fun!
    Last edited by ramin.honary; 08-17-2011 at 04:58 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •