Python script to grep fields and use values from file 1 as argument for another command


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Python script to grep fields and use values from file 1 as argument for another command

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 :
Code:
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

Quote:
show sub data-rate smgr-instance 5025
Quote:
show sub data-rate smgr-instance 9
Quote:
show sub data-rate smgr-instance 38
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:
Code:
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:
Code:
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:

Code:
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


Last edited by as7951; 07-17-2019 at 03:33 AM..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. Programming

Python script to run multiple command and append data in output csv file

Experts, I am writing a script and able to write only small piece of code and not able to collect logic to complete this task. In input file have to look for name like like this (BGL_HSR_901_1AG_A_CR9KTR10) before sh iss neors. Record this (BGL_HSR_901_1AG_A_CR9KTR10) in csv file Now have to... (0 Replies)
Discussion started by: as7951
0 Replies

2. Programming

Python or Shell script to Grep strings from input file and output in csv format

Hi Experts, I am writing a python script to grep string from file and display output in csv file as in attached screenshot https://drive.google.com/file/d/1gfUUdfmQma33tz65NskThYDhkZUGQO0H/view Input file(result_EPFT_config_device) Below is the python script i have prepared as of... (1 Reply)
Discussion started by: as7951
1 Replies

3. Shell Programming and Scripting

How to identify varying unique fields values from a text file in UNIX?

Hi, I have a huge unsorted text file. We wanted to identify the unique field values in a line and consider those fields as a primary key for a table in upstream system. Basically, the process or script should fetch the values from each line that are unique compared to the rest of the lines in... (13 Replies)
Discussion started by: manikandan23
13 Replies

4. Shell Programming and Scripting

awk to print line is values between two fields in separate file

I am trying to use awk to find all the $3 values in file2 that are between $2 and $3 in file1. If a value in $3 of file2 is between the file1 fields then it is printed along with the $6 value in file1. Both file1 and file2 are tab-delimited as well as the desired output. If there is nothing to... (4 Replies)
Discussion started by: cmccabe
4 Replies

5. Shell Programming and Scripting

Fields shifting in file, do to null values?

The below code runs and creates an output file with three sections. The first 2 sections are ok, but the third section doesn't seem to put a . in all the fields that are blank. I don't know if this is what causes the last two fields in the current output to shift to a newline, but I can not seem... (3 Replies)
Discussion started by: cmccabe
3 Replies

6. Shell Programming and Scripting

Pass File name and Directory Path through command to python script

I'm writing python script to get the file-names in the current directory and file sizes .I'm able to get file list and their sizes but unable to pass them through command line. I want to use this script to execute on other directory and pass directory path with file name through command line. Any... (1 Reply)
Discussion started by: etldeveloper
1 Replies

7. Shell Programming and Scripting

Make script that run with argument if not run from configuration file argument

Hello, Is there any method thorugh which script can take argument if pass otherwise if argument doesn't pass then it takes the argument from the configuration file i.e I am workiing on a script which will run through crontab and the script will chekout the code ,zip and copy to the... (3 Replies)
Discussion started by: rohit22hamirpur
3 Replies

8. Shell Programming and Scripting

Getting Values from Command Line in a script file.

Hi All, I am new to shell scripting. Is there anyway of getting parameters from command line in shell script wherever required? Thanks in advance. (1 Reply)
Discussion started by: dirshah
1 Replies

9. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with -

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

10. Shell Programming and Scripting

Handling values with space while passing commandline argument from wrapper script in KSH

Hi there, I have a wapper script which passes the argument from command prompt to inner script.. It works fine as long as the argument containing single word. But when value contains multiple word with space, not working as expected. I tried my best, couldn't find the reason. Gurus, pls.... (2 Replies)
Discussion started by: kans
2 Replies

Featured Tech Videos