PDA

View Full Version : [ubuntu] need a help to extract the content form text file and do some mathematical calculatio



jpjohnj
August 16th, 2011, 10:55 AM
Output.txt file has>>>>>>>>>


------------------------------------------------------------
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
------------------------------------------------------------
Client connecting to 192.168.1.3, TCP port 5001
TCP window size: 16.0 KByte (default)
------------------------------------------------------------
[ 3] local 192.168.1.4 port 35207 connected with 192.168.1.3 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.2 sec 16.4 MBytes 13.4 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 35208 connected with 192.168.1.3 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.1 sec 15.0 MBytes 12.5 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 35209 connected with 192.168.1.3 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.0 sec 11.8 MBytes 9.83 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 35213 connected with 192.168.1.3 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.0 sec 12.0 MBytes 10.0 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 35218 connected with 192.168.1.3 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.1 sec 11.8 MBytes 9.73 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 35222 connected with 192.168.1.3 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.1 sec 12.4 MBytes 10.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 35230 connected with 192.168.1.3 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.1 sec 16.0 MBytes 13.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 35280 connected with 192.168.1.3 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.1 sec 12.2 MBytes 10.2 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 35308 connected with 192.168.1.3 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.1 sec 11.5 MBytes 9.58 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 35333 connected with 192.168.1.3 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.1 sec 12.5 MBytes 10.4 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 35351 connected with 192.168.1.3 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.0 sec 15.0 MBytes 12.6 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 35354 connected with 192.168.1.3 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.2 sec 16.4 MBytes 13.5 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 35355 connected with 192.168.1.3 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.1 sec 12.0 MBytes 9.98 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 35356 connected with 192.168.1.3 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.1 sec 11.8 MBytes 9.73 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 48254 connected with 192.168.1.3 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.0 sec 11.2 MBytes 9.39 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 48255 connected with 192.168.1.3 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.0 sec 12.6 MBytes 10.5 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 48258 connected with 192.168.1.3 port 5001
[ ID] Interval Transfer Bandwidth
[ 3] 0.0-10.1 sec 11.5 MBytes 9.55 Mbits/sec


...........................................





Here i need to extract the value bellow in the bandwidth for every connection and i need to add all the bandwidth value >>>


help me to do that.....

BinaryEnCoder
August 16th, 2011, 12:01 PM
you can use some regex in perl but i dont think this has something to do with ubuntu

try googling perl regex and make some patterns for the filters you need
and filter the output.txt

aeiah
August 16th, 2011, 12:56 PM
you could do it loads of different ways. in a bash script you could use grep, trim, awk and such.

or you could do it in python:

import sys

# open the file specified as the argument
file = open(sys.argv[1], 'r')

# variable the total will be stored
total = 0

#iterate over each line in the file
for line in file:
#find the lines we need
if 'MBytes' in line:
#parse the number we want out of the line, discard the rest
mb = line.split(' sec ')[1].split(' MBytes')[0]
# add it to the total
total = total+float(mb)


print total



save it as totalbandwidth.py and call it on the commandline with
python totalbandwidth.py output.txt

if you want the output saved somewhere, pipe it:

python totalbandwidth.py output.txt > total.txt