Hi Experts,
I am working one one python script in version 3.x and 2.6. Need your support to complete it
Basically for both commands i have telnet to device and run command and then receiving input File 1 and File 2
I have two commands, need to grep data and output in csv file.
Next script/code where want to use values from third column "inst"(Input file 1) and have to pass in other command as below and grep string and output in another file
Input File1 :
cpu facility inst used allc used alloc used allc used allc
1/0 sessmgr 5025 0.23% 50% 98.90M 230.0M 31 500 -- --
1/0 sessmgr 9 31% 100% 639.1M 900.0M 41 500 3588 12000
1/0 sessmgr 38 30% 100% 538.3M 900.0M 37 500 3580 12000
1/0 sessmgr 52 29% 100% 533.7M 900.0M 37 500 3592 12000
1/0 sessmgr 75 28% 100% 548.5M 900.0M 35 500 3587 12000
1/0 sessmgr 91 29% 100% 539.1M 900.0M 35 500 3578 12000
1/0 sessmgr 102 27% 100% 544.1M 900.0M 36 500 3580 12000
1/0 sessmgr 129 30% 100% 545.1M 900.0M 35 500 3585 12000
1/0 sessmgr 150 27% 100% 542.8M 900.0M 34 500 3573 12000
1/0 sessmgr 172 32% 100% 540.3M 900.0M 33 500 3580 12000
1/0 sessmgr 177 27% 100% 541.9M 900.0M 32 500 3583 12000
1/0 sessmgr 196 29% 100% 535.8M 900.0M 32 500 3575 12000
1/0 sessmgr 279 29% 100% 548.3M 900.0M 33 500 3593 12000
1/0 sessmgr 285 30% 100% 539.5M 900.0M 33 500 3585 12000
1/0 sessmgr 294 31% 100% 541.9M 900.0M 32 500 3589 12000
1/0 sessmgr 302 34% 100% 536.7M 900.0M 35 500 3598 12000
1/0 sessmgr 325 31% 100% 535.9M 900.0M 36 500 3582 12000
1/0 sessmgr 327 31% 100% 538.8M 900.0M 36 500 3580 12000
1/0 sessmgr 364 31% 100% 539.9M 900.0M 38 500 3599 12000
Now, want to use values from third column inst and have to pass in other command as below
This above command will give me output as shown in below Input File 2.
here only pasting one output from above command, similary other two commands will give similar output.
and have to keep only line "ave rate from user(bps)" as column heading(uplink througput) and "17104773" as its value in row and similarly "ave rate to user(bps)" in another column with heading(Downlink throughput) and "247272821" its value in row in another CSV FILE.
Input File 2:
Thursday July 11 17:10:45 IST
Total Subscribers : 2978
Active : 2978 Dormant : 0
peak rate from user(bps): n/a* peak rate to user(bps) : n/a*
ave rate from user(bps) : 17104773 ave rate to user(bps) : 247272821
sust rate from user(bps): 16999794 sust rate to user(bps) : 246777077
peak rate from user(pps): n/a* peak rate to user(pps) : n/a*
ave rate from user(pps) : 13117 ave rate to user(pps) : 27879
sust rate from user(pps): 13233 sust rate to user(pps) : 27976
Expected output from Input file 2 in csv file:
sessmgr instance,uplink througput,Downlink throughput
5025 17104773, 247272821
9 17104773, 247272821
38 17104773, 247272821
What I have tried:
As of now only able to fetch expected output required from Input File 2:
with open("infile.txt") as infile, open("outfile.csv", "w") as outfile:
data = infile.read() #Read infile content
ave_rate_from_user = re.search(r"ave rate from user\(bps\)\s*:\s*(\d+)\b", data) #Search for `ave rate from user`
if ave_rate_from_user:
ave_rate_from_user = ave_rate_from_user.group(1)
ave_rate_to_user = re.search(r"ave rate to user\(bps\)\s*:\s*(\d+)\b", data) #Search for `ave rate to user`
if ave_rate_to_user:
ave_rate_to_user = ave_rate_to_user.group(1)
writer = csv.writer(outfile)
writer.writerow(["ave rate from user(bps)", "ave rate to user(bps)"]) #Write Header
writer.writerow([ave_rate_from_user, ave_rate_to_user]) #Write Content