Python insert a line just above the second occurrence of a pattern from end of file [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have a RHEL kickstart file as below with multiple %post ... %end sections in it having many lines of Python code in between them.



%post
.....
.....
%end
%post
.....
.....
%end
%post
.....
.....
%end


I am trying to insert a new line with some data just before (above) the 2nd occurrence of search pattern "%end" from last (EOF). Any hints/input is really appreciated.







share|improve this question













closed as unclear what you're asking by Jeff Schaller, Jesse_b, roaima, G-Man, nwildner May 10 at 17:05


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • how about Unix command-line tools?
    – RomanPerekhrest
    May 9 at 13:20










  • @don_crissti I tried with findall() but gives list of the patterns from that I have used -2 index to retrieve the 2nd occurrence of pattern from last, but unable to fetch the line no above this 2nd occurrence of the pattern
    – Ibrahim Quraish
    May 9 at 14:24










  • @RomanPerekhrest, I used the code something like, tac file | awk -v n=0 '/%end/ n++; if (n==3) print FNR-1'
    – Ibrahim Quraish
    May 9 at 14:26










  • What's the relevance of Python in the title?
    – roaima
    May 9 at 22:21










  • @roaima I have to do it via Python to insert some python code into this target kickstart file which contains python code. I am thinking of using shell script is to be imbibed in Python's subprocess.Popen()
    – Ibrahim Quraish
    May 10 at 6:08















up vote
0
down vote

favorite












I have a RHEL kickstart file as below with multiple %post ... %end sections in it having many lines of Python code in between them.



%post
.....
.....
%end
%post
.....
.....
%end
%post
.....
.....
%end


I am trying to insert a new line with some data just before (above) the 2nd occurrence of search pattern "%end" from last (EOF). Any hints/input is really appreciated.







share|improve this question













closed as unclear what you're asking by Jeff Schaller, Jesse_b, roaima, G-Man, nwildner May 10 at 17:05


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • how about Unix command-line tools?
    – RomanPerekhrest
    May 9 at 13:20










  • @don_crissti I tried with findall() but gives list of the patterns from that I have used -2 index to retrieve the 2nd occurrence of pattern from last, but unable to fetch the line no above this 2nd occurrence of the pattern
    – Ibrahim Quraish
    May 9 at 14:24










  • @RomanPerekhrest, I used the code something like, tac file | awk -v n=0 '/%end/ n++; if (n==3) print FNR-1'
    – Ibrahim Quraish
    May 9 at 14:26










  • What's the relevance of Python in the title?
    – roaima
    May 9 at 22:21










  • @roaima I have to do it via Python to insert some python code into this target kickstart file which contains python code. I am thinking of using shell script is to be imbibed in Python's subprocess.Popen()
    – Ibrahim Quraish
    May 10 at 6:08













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a RHEL kickstart file as below with multiple %post ... %end sections in it having many lines of Python code in between them.



%post
.....
.....
%end
%post
.....
.....
%end
%post
.....
.....
%end


I am trying to insert a new line with some data just before (above) the 2nd occurrence of search pattern "%end" from last (EOF). Any hints/input is really appreciated.







share|improve this question













I have a RHEL kickstart file as below with multiple %post ... %end sections in it having many lines of Python code in between them.



%post
.....
.....
%end
%post
.....
.....
%end
%post
.....
.....
%end


I am trying to insert a new line with some data just before (above) the 2nd occurrence of search pattern "%end" from last (EOF). Any hints/input is really appreciated.









share|improve this question












share|improve this question




share|improve this question








edited May 10 at 6:08
























asked May 9 at 12:43









Ibrahim Quraish

1419




1419




closed as unclear what you're asking by Jeff Schaller, Jesse_b, roaima, G-Man, nwildner May 10 at 17:05


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by Jeff Schaller, Jesse_b, roaima, G-Man, nwildner May 10 at 17:05


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.













  • how about Unix command-line tools?
    – RomanPerekhrest
    May 9 at 13:20










  • @don_crissti I tried with findall() but gives list of the patterns from that I have used -2 index to retrieve the 2nd occurrence of pattern from last, but unable to fetch the line no above this 2nd occurrence of the pattern
    – Ibrahim Quraish
    May 9 at 14:24










  • @RomanPerekhrest, I used the code something like, tac file | awk -v n=0 '/%end/ n++; if (n==3) print FNR-1'
    – Ibrahim Quraish
    May 9 at 14:26










  • What's the relevance of Python in the title?
    – roaima
    May 9 at 22:21










  • @roaima I have to do it via Python to insert some python code into this target kickstart file which contains python code. I am thinking of using shell script is to be imbibed in Python's subprocess.Popen()
    – Ibrahim Quraish
    May 10 at 6:08

















  • how about Unix command-line tools?
    – RomanPerekhrest
    May 9 at 13:20










  • @don_crissti I tried with findall() but gives list of the patterns from that I have used -2 index to retrieve the 2nd occurrence of pattern from last, but unable to fetch the line no above this 2nd occurrence of the pattern
    – Ibrahim Quraish
    May 9 at 14:24










  • @RomanPerekhrest, I used the code something like, tac file | awk -v n=0 '/%end/ n++; if (n==3) print FNR-1'
    – Ibrahim Quraish
    May 9 at 14:26










  • What's the relevance of Python in the title?
    – roaima
    May 9 at 22:21










  • @roaima I have to do it via Python to insert some python code into this target kickstart file which contains python code. I am thinking of using shell script is to be imbibed in Python's subprocess.Popen()
    – Ibrahim Quraish
    May 10 at 6:08
















how about Unix command-line tools?
– RomanPerekhrest
May 9 at 13:20




how about Unix command-line tools?
– RomanPerekhrest
May 9 at 13:20












@don_crissti I tried with findall() but gives list of the patterns from that I have used -2 index to retrieve the 2nd occurrence of pattern from last, but unable to fetch the line no above this 2nd occurrence of the pattern
– Ibrahim Quraish
May 9 at 14:24




@don_crissti I tried with findall() but gives list of the patterns from that I have used -2 index to retrieve the 2nd occurrence of pattern from last, but unable to fetch the line no above this 2nd occurrence of the pattern
– Ibrahim Quraish
May 9 at 14:24












@RomanPerekhrest, I used the code something like, tac file | awk -v n=0 '/%end/ n++; if (n==3) print FNR-1'
– Ibrahim Quraish
May 9 at 14:26




@RomanPerekhrest, I used the code something like, tac file | awk -v n=0 '/%end/ n++; if (n==3) print FNR-1'
– Ibrahim Quraish
May 9 at 14:26












What's the relevance of Python in the title?
– roaima
May 9 at 22:21




What's the relevance of Python in the title?
– roaima
May 9 at 22:21












@roaima I have to do it via Python to insert some python code into this target kickstart file which contains python code. I am thinking of using shell script is to be imbibed in Python's subprocess.Popen()
– Ibrahim Quraish
May 10 at 6:08





@roaima I have to do it via Python to insert some python code into this target kickstart file which contains python code. I am thinking of using shell script is to be imbibed in Python's subprocess.Popen()
– Ibrahim Quraish
May 10 at 6:08











2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Python snippet that inserts text spam at the last position in the second-to-last %post..%end block:



with open('input') as fp:
lines = fp.readlines()

locs = [i for i, val in enumerate(lines) if val == '%endn']
lines.insert(locs[-2], 'spam' + 'n')

with open('input', 'w') as fp:
fp.write(''.join(lines))





share|improve this answer




























    up vote
    1
    down vote













    To insert "some data" before next to last pattern line.
    grep + awk solution:



    awk -v n="$(($(grep -c '^%end' inputfile) - 1))" 
    '/^%end/ if (++cnt == n) print "some data" 1' inputfile


    The output:



    %post
    .....
    .....
    %end
    %post
    .....
    .....
    some data
    %end
    %post
    .....
    .....
    %end





    share|improve this answer




























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      Python snippet that inserts text spam at the last position in the second-to-last %post..%end block:



      with open('input') as fp:
      lines = fp.readlines()

      locs = [i for i, val in enumerate(lines) if val == '%endn']
      lines.insert(locs[-2], 'spam' + 'n')

      with open('input', 'w') as fp:
      fp.write(''.join(lines))





      share|improve this answer

























        up vote
        1
        down vote



        accepted










        Python snippet that inserts text spam at the last position in the second-to-last %post..%end block:



        with open('input') as fp:
        lines = fp.readlines()

        locs = [i for i, val in enumerate(lines) if val == '%endn']
        lines.insert(locs[-2], 'spam' + 'n')

        with open('input', 'w') as fp:
        fp.write(''.join(lines))





        share|improve this answer























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Python snippet that inserts text spam at the last position in the second-to-last %post..%end block:



          with open('input') as fp:
          lines = fp.readlines()

          locs = [i for i, val in enumerate(lines) if val == '%endn']
          lines.insert(locs[-2], 'spam' + 'n')

          with open('input', 'w') as fp:
          fp.write(''.join(lines))





          share|improve this answer













          Python snippet that inserts text spam at the last position in the second-to-last %post..%end block:



          with open('input') as fp:
          lines = fp.readlines()

          locs = [i for i, val in enumerate(lines) if val == '%endn']
          lines.insert(locs[-2], 'spam' + 'n')

          with open('input', 'w') as fp:
          fp.write(''.join(lines))






          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered May 9 at 15:58









          hoefling

          418410




          418410






















              up vote
              1
              down vote













              To insert "some data" before next to last pattern line.
              grep + awk solution:



              awk -v n="$(($(grep -c '^%end' inputfile) - 1))" 
              '/^%end/ if (++cnt == n) print "some data" 1' inputfile


              The output:



              %post
              .....
              .....
              %end
              %post
              .....
              .....
              some data
              %end
              %post
              .....
              .....
              %end





              share|improve this answer

























                up vote
                1
                down vote













                To insert "some data" before next to last pattern line.
                grep + awk solution:



                awk -v n="$(($(grep -c '^%end' inputfile) - 1))" 
                '/^%end/ if (++cnt == n) print "some data" 1' inputfile


                The output:



                %post
                .....
                .....
                %end
                %post
                .....
                .....
                some data
                %end
                %post
                .....
                .....
                %end





                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  To insert "some data" before next to last pattern line.
                  grep + awk solution:



                  awk -v n="$(($(grep -c '^%end' inputfile) - 1))" 
                  '/^%end/ if (++cnt == n) print "some data" 1' inputfile


                  The output:



                  %post
                  .....
                  .....
                  %end
                  %post
                  .....
                  .....
                  some data
                  %end
                  %post
                  .....
                  .....
                  %end





                  share|improve this answer













                  To insert "some data" before next to last pattern line.
                  grep + awk solution:



                  awk -v n="$(($(grep -c '^%end' inputfile) - 1))" 
                  '/^%end/ if (++cnt == n) print "some data" 1' inputfile


                  The output:



                  %post
                  .....
                  .....
                  %end
                  %post
                  .....
                  .....
                  some data
                  %end
                  %post
                  .....
                  .....
                  %end






                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered May 9 at 15:25









                  RomanPerekhrest

                  22.4k12144




                  22.4k12144












                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay