0

I have multiple text files as below:

I want to read all of the text files and find the IP value in each file and replace it with the noresult string which is available in the text and save per file in python.

Text1

     Id                = 0005      
     Cause          = ERROR      
     Code     = 307      
     Event Time              = 2020-11-09 10:16:48      
     Severity      = WARNING      
     Severity Code = 5     
     result = noresult
     Id                = 0006      
     Cause          = FAILURE      
     Code     = 517      
     Event Time              = 2020-11-09 10:19:47      
     Severity      = MINOR      
     Severity Code = 4 result = noresult 
     ip[10.1.1.1

Text2

 Id                = 0007      
 Cause          = ERROR      
 Code     = 307      
 Event Time              = 2020-11-09 10:16:48      
 Severity      = WARNING      
 Severity Code = 5      
 Id                = 0008      
 Cause          = FAILURE 
 result = noresult     
 Code     = 517      
 Event Time              = 2020-11-09 10:19:47      
 Severity      = MINOR      
 Severity Code = 4  
 result = noresult
 ip[10.1.1.3

Needful result:

Text1

 Id                = 0005      
 Cause          = ERROR      
 Code     = 307      
 Event Time              = 2020-11-09 10:16:48      
 Severity      = WARNING      
 Severity Code = 5     
 result = 10.1.1.1
 Id                = 0006      
 Cause          = FAILURE      
 Code     = 517      
 Event Time              = 2020-11-09 10:19:47      
 Severity      = MINOR      
 Severity Code = 4 result = 10.1.1.1
 ip[10.1.1.1

Text2

 Id                = 0007      
 Cause          = ERROR      
 Code     = 307      
 Event Time              = 2020-11-09 10:16:48      
 Severity      = WARNING      
 Severity Code = 5      
 Id                = 0008      
 Cause          = FAILURE 
 result = 10.1.1.3
 Code     = 517      
 Event Time              = 2020-11-09 10:19:47      
 Severity      = MINOR      
 Severity Code = 4  
 result = 10.1.1.3
 ip[10.1.1.3

1 Answer 1

0

If the ip is always your last field you can simply do this:

txt = txt.replace("noresult", txt.split("ip[")[-1])

In details, if you want to read, modify and write:

txt = open("filename.txt", "r").read()
txt = txt.replace("noresult", txt.split("ip[")[-1])
open("filename.txt", "w").write(txt)

If you have more files, you can group their paths in a list file_list = ["file1.txt", "file2.txt", ... ] and then repeat the above procedures cycling on this list:

for filepath in file_list:
    txt = open(filepath, "r").read()
    txt = txt.replace("noresult", txt.split("ip[")[-1])
    open(filepath, "w").write(txt)
2
  • This works great! I appreciate if you let me know how I can make this change in all text files and save them also, sorry if i am so new to python.
    – BrainGain
    May 11, 2022 at 11:07
  • Sure, but I need to know if you have a list of the path of the files you want to modify. May 11, 2022 at 13:21

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.