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

Prince Sharma prince09cs at gmail.com
Fri Apr 17 09:16:12 CEST 2015


For that you should compile the regex with infrastructure and platform
together. And have that check before checks for infrastructure and platform
separately.
 On Apr 17, 2015 12:10 PM, "arvind ks" <arvind.ks1985 at gmail.com> wrote:

> Hi Kishore,
> Thanks for the reply. I've tried adding some additional checks, but unable
> to understand where i am making a mistake.
> Could you please check and advise.
>
> Input::
>
> 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("input.txt","r")
> f2=open("out.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):
>                 if reg5.match(cl) and reg2.match(nl) and reg4.match(nnl):
>                         f2.write(cl.rstrip("\n") + " H" + ("\n"))
>                 if reg5.match(cl) and reg2.match(nnl) and reg4.match(nl):
>                         f2.write(cl.rstrip("\n") + " H" + ("\n"))
>                 if reg5.match(cl) and reg3.match(nl) and reg4.match(nnl):
>                         f2.write(cl.rstrip("\n") + " A" + ("\n"))
>                 if reg5.match(cl) and reg3.match(nnl) and reg4.match(nl):
>                         f2.write(cl.rstrip("\n") + " A" + ("\n"))
>         else:
>                 f2.write(cl)
>
> f1.close()
> f2.close()
>
>
> Output::
> define service{
>         use generic-service
>         service_description CPU Load - H
>         service_description CPU Load - A
>         check_command check_nrpe_cpu
>         contact_groups Infrastructure,Platforms
> }
>
>
>
>
> I am again getting 2 seperate lines. It should ideally be in a single line
> i.e. H A as there are 2 entries in contact_groups lines i.e. Infrastructure
> and Platforms.
>
> Thanks
> Arvind
>
> On Fri, Apr 17, 2015 at 11:41 AM, Kishor Bhat <kishorbhat at gmail.com>
> wrote:
>
> > 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
> > _______________________________________________
> > BangPypers mailing list
> > BangPypers at python.org
> > https://mail.python.org/mailman/listinfo/bangpypers
> >
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> https://mail.python.org/mailman/listinfo/bangpypers
>


More information about the BangPypers mailing list