[BangPypers] Need help with adding a character at the end of the line in a file in linux.

arvind ks arvind.ks1985 at gmail.com
Fri Apr 17 08:05:41 CEST 2015


Hi Everyone,
Could you please help me with the below python script that tries to add
characters A H to line starting with "service description" depending on
contact groups.

*Input file::*

define service{
use generic-service
service_description CPU Load -
check_command check_nrpe_cpu
contact_groups Infrastructure,Platforms
}

*Script::*

#!/usr/bin/python
import re,sys,os,string
f1=open("carpet.txt","r")
f2=open("carpet1.txt","w")
reg1=re.compile("^\s+contact_groups")
reg2=re.compile(".*Infrastructure.*")
reg3=re.compile(".*Platforms.*")
reg4=re.compile("^\s+check_command")
reg5=re.compile("^\s+service_description")
lines=f1.readlines()
length=len(lines)
length_4_nl=length - 1
length_4_nnl=length - 2


for i in range(0,length,1):
j = i - 1
k = i + 1
l = i + 2
cl = lines[i]
pl = lines[j]

if i < length_4_nl:
nl = lines[k]
if i < length_4_nnl:
nnl = lines[l]

if reg5.match(cl) and reg2.match(nl):
f2.write(cl.rstrip("\n") + " H" + ("\n"))
if reg5.match(cl) and reg3.match(nl):
f2.write(cl.rstrip("\n") + " A" + ("\n"))
#else:
# f2.write(cl)

f1.close()
f2.close()


*Output::*

service_description NRPE Check Load - 1min/5min/15min - H
service_description NRPE Check Load - 1min/5min/15min - A

*Issue::*
I want the output to be

service_description NRPE Check Load - 1min/5min/15min - H A

i.e. if the i have a tag "H" for Infrastructure and "A" for "Platforms".
I am trying to append it to the same line rather than creating a new line.
But it doesnt work. Please help


More information about the BangPypers mailing list