I have a .txt file include many duplicate lines, and I want to replace the first line and keep the others. Can anyone help me?
the original test.txt content
222
111
111
111
111
the file I want
222
111
111
111
I have tried this method
But this method will replace all the duplicate lines.
Anyway, I get the answer. It's really simple.
flag = 1
for line in fileinput.input(filename, inplace = 1):
if "111" in line and flag==1:
print(line.replace("111", "22222").rstrip() )
flag = 2
else:
print(line.replace("111", "111").rstrip() )
I think that is not efficient and hope your answer.