Python insert a line just above the second occurrence of a pattern from end of file [closed]
Clash 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.
python
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.
 |Â
show 1 more comment
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.
python
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'ssubprocess.Popen()
â Ibrahim Quraish
May 10 at 6:08
 |Â
show 1 more comment
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.
python
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.
python
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'ssubprocess.Popen()
â Ibrahim Quraish
May 10 at 6:08
 |Â
show 1 more comment
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'ssubprocess.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
 |Â
show 1 more comment
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))
add a comment |Â
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
add a comment |Â
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))
add a comment |Â
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))
add a comment |Â
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))
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))
answered May 9 at 15:58
hoefling
418410
418410
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered May 9 at 15:25
RomanPerekhrest
22.4k12144
22.4k12144
add a comment |Â
add a comment |Â
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