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

Kishor Bhat kishorbhat at gmail.com
Fri Apr 17 08:11:25 CEST 2015


On Fri, Apr 17, 2015 at 11:35 AM, arvind ks <arvind.ks1985 at gmail.com> wrote:
> 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"))

You're writing the line to the file twice.
Try changing your conditions; something along the lines of 'check for
"HA", else "H", else "A".' should work.

Regards,
Kishor

> #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
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> https://mail.python.org/mailman/listinfo/bangpypers


More information about the BangPypers mailing list