Newbie here and I'd really appreciate your help in order for me to determine whats the best way to go with below scenario. Please note I'm using python 2.7.X
I've got a file "file.txt" in a centos 7 box which has following lines in it. (Pay attention to the new lines)
Code:
blah blah blah
ID:name1:1bj409ju9
how are you
Im good 100
blah blah
ID:name2:987krjtu
not so good
too bad 900
blah blah
some words blah blah
As you notice the lines start with "ID" have a pattern. My attempt is to search for ID:name[x] and delete 5 lines (including the space). for an instance say i want to delete below set of lines from the file.
Code:
ID:name1:10.1.1.10
how are you
I'm good 100
blah blah
I tried the following code but it deletes only the lines matching "somename1". Please advice.
Code:
#!/usr/bin/python
import fileinput
filename = r"file.txt"
counter = -1
for linenum,line in enumerate(fileinput.FileInput(filename, inplace=1)):
if "name1" in line:
counter = linenum + 6
if linenum == counter:
line.strip()
else:
print line,
Please note that I want to get rid of the new empty line in between "blah blah" and "ID:name2:987krjtu". Please help.