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


 
Thread Tools Search this Thread
Top Forums Programming Python script to run multiple command and append data in output csv file
# 1  
Old 08-11-2019
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 grep field with names like(BGL_MWN_901_1AC_T_CR9R1) line above IS-extended, pick one of it suppose(BGL_RGN_901_1AC_T_CR9R5) also record this in output csv file.

Now from the next two Is-extended grep field (BGL_CCG_901_1AG_CR9R13.00) also record it in csv file and BGL_CCG_902_1AC_CR9R5 already taken so have to skip it.
Now from next two Is-extended grep field (BGL_CCG_902_1AC_CR9R14) also record it in csv file and BGL_CCG_902_1AC_CR9R13 already taken so have to skip it.
Now from next two Is-extended grep field (BGL_CCG_902_1AC_CR9KT04.00) also record it in csv file and BGL_CCG_902_1AC_CR9R14 already taken so have to skip it. Here we have to stop search when we have find field having "9K" in it and create row like below as in output file

Now again pick another value/field from line above IS-extended, record it in output file and repeat the same process and build another row in output file
Now, In line IS-extended field (BGL_CCG_901_1AG_CR9K41) have 9K in it. so have to pick it and record in csv file and stop search

Now again pick another value/field from line above IS-extended and repeat the same process and build another row in output file

output csv file:
Code:
BGL_HSR_901_1AG_A_CR9KTR10,BGL_RGN_901_1AC_T_CR9R5,BGL_CCG_901_1AG_CR9R13,BGL_CCG_902_1AC_CR9R14,BGL_CCG_902_1AC_CR9KT04
BGL_HSR_901_1AG_A_CR9KTR10,BGL_VKP_901_1AC_CR9R1,BGL_CCG_901_1AG_CR9K41
BGL_HSR_901_1AG_A_CR9KTR10,BGL_HSR_943_1AC_B_CR9R7


Input file
Code:
RP/:BGL_HSR_901_1AG_A_CR9KTR10#sh iss neors 
    
    BGL_MWN_901_1AC_T_CR9R1 0/0/        *P*         U    
    BGL_VKP_901_1AC_CR9R1 0/1/        *P*         U          
    BGL_RGN_901_1AC_T_CR9R5 0/1/        *PP*         p        
       
    BGL_HSR_943_1AC_B_CR9R7 /0/7        *P*         p     
    BGL_HSR_943_1AC_B_CR9R7 /0/4        *P*         p           
    BGL_HSR_944_1AC_B_CR9R5 
    
      Metc: 10         IS-Extended BGL_CCG_902_1AC_CR9R5.00
      Meic: 10         IS-Extended BGL_CCG_901_1AG_CR9R13.00
    
    
      Metc: 10         IS-Extended BGL_CCG_902_1AC_CR9R14.00
      Meic: 10         IS-Extended BGL_CCG_901_1AG_CR9R13.00  
    
     
      Metc: 10         IS-Extended BGL_CCG_902_1AC_CR9KT04.00
      Meic: 10         IS-Extended BGL_CCG_902_1AC_CR9R14.00 
    
      Metc: 10         IS-Extended BGL_VKP_901_1AC_CR9R1.00
      Meic: 10         IS-Extended BGL_CCG_901_1AG_CR9K41.00

My code :

Code:
 import csv
    import os
    import re
    
    
    data = []
    tmp = ''
    output = ''
    fieldnames = ("first")
    
    infile = open("sample.txt", "r")
    #outfile = open("outfile.csv", "w")
    
    for datas in infile:
        datas = datas.rstrip("\n")
        a = re.search(r"([A-Z]+_[A-Z]+_[0-9]+_[0-9][A-Z]+\D*_[A-Z]+[0-9]+[A-Z]+[0-9]+)", datas)
        if a:
            a = a.group(1)
            tmp = a
            output = output+'\n'+tmp
            data = output
        with open('your_file.csv', 'w') as f:
    #        for item in output:
    #        print >> f, item
                f.writelines(data)


Last edited by as7951; 08-12-2019 at 02:22 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Need a script to parse data and output to csv

I am not too savvy with arrays and am assuming that what I am looking for needs arrays. This is my requirement. So I have the raw data that gets updated to a log as shown below StudentInfo: FullInfo = { Address = Newark Age = 20 Name= John } StudentInfo:... (2 Replies)
Discussion started by: sidnow
2 Replies

3. Shell Programming and Scripting

Pass some data from csv to xml file using shell/python

Hello gurus, I have a csv file with bunch of datas in each column. (see attached) Now I have an .xml file in the structure of below: ?xml version="1.0" ?> <component id="root" name="root"> <component id="system" name="system"> <param name="number_of_A" value="8"/> ... (5 Replies)
Discussion started by: Zam_1234
5 Replies

4. Shell Programming and Scripting

Bash script with python slicing on multiple data files

I have 2 files generated in linux that has common output and were produced across multiple hosts with the same setup/configs. These files do some simple reporting on resource allocation and user sessions. So, essentially, say, 10 hosts, with the same (2) system reporting in the files, so a... (0 Replies)
Discussion started by: jdubbz
0 Replies

5. Shell Programming and Scripting

Read multiple files, parse data and append to a file

Hi..Can anyone suggest a simple way of achieving this. I have several files which ends with extension .vcf . I will give example with two files In the below files, we are interested in File 1: 38 107 C 3 T 6 C/T 38 241 C 4 T 5 C/T 38 247 T 4 C 5 T/C 38 259 T 3 C 6 T/C... (8 Replies)
Discussion started by: empyrean
8 Replies

6. Shell Programming and Scripting

Append data to new row in CSV file every day

Hi All I will run the same script every day in corn and output should go to same CSV file but in different row with dates on it. Below is my example in attached format. Script i am using to collect switch port online DATE=`date '+%d-%m-%y'` for f in `cat... (1 Reply)
Discussion started by: ranjancom2000
1 Replies

7. Shell Programming and Scripting

Script to run command against multiple specific value in one file

Hi all, I am trying to create a shell script from solaris 10 server to run a command into multiple specific value in one file. The command is related to Oracle/Sun JES2005Q4 directory server. #this is the command, #from path /jes/ds/slapd-rldap1 ./ns-inactivate.pl -h mldap1 -p 389 -D... (12 Replies)
Discussion started by: Mr_47
12 Replies

8. Shell Programming and Scripting

Shell script to run a python program on multiple entries in a file

Hello I am trying to run a python program using shell script, which takes a single argument from a file. This file has one entry per line : 1aaa 2bbb 3ccc 4ddd 5eee ... ... ... My shell script runs the program, only for the last entry : #!/bin/sh IFS=$'\n' for line in $(cat... (2 Replies)
Discussion started by: ad23
2 Replies

9. Shell Programming and Scripting

I want to append data to same .csv file.

I have a script which has to be scheduled to run 3 times a day. My script picks the required fields from logfile and stores the data in a.csv file. Sample data. my logfile contain: 0097A,0374D,100903,1519,00000606191 0097A,C88RA,100903,0724,00000606105 So the output of first execution... (3 Replies)
Discussion started by: shrima.pratima
3 Replies

10. Shell Programming and Scripting

How to append value at first line of CSV file using shell script?

I have an issue where I need to append a value at the last of the csv, I have created a shell script and it is appending the columns at the last but it is appending at all lines, and my requirement is specific to just append at the 1st line. Have a look and suggest, (7 Replies)
Discussion started by: anujrichhariya
7 Replies
Login or Register to Ask a Question