import re infile = open('document.txt','r') outfile= open('output.txt','w') copy = False for line in infile:
if line.strip() == "--operation():": bucket = [] copy = True elif line.strip() == "StartOperation": for strings in bucket: outfile.write( strings + ',') for strings in bucket: outfile.write('\n') copy = False elif copy: bucket.append(line.strip() -------------------------------------- CSV format is like this: id, name, poid, error 5896, AutoAuthOSUserSubmit, 900105270, 0x4002 My log file has several sections starting with ==== START ==== and ending with ==== END ====. I want to extract the string between --operation(): and StartOperation. For example, AutoAuthOSUserSubmit. I also want to extract the poid value from line poid: 900105270, poidLen: 9. Finally, I want to extract the return value, e.g 0x4002 if Roll back all updates is found after it. I am not even able to extract point the original text if Start and End are not on the same line. How do I go about doing that? This is a sample LOG extract with two paragraphs: -- 08/24 02:07:56 [mds.ecas(5896) ECAS_CP1] **==== START ====** open file /ecas/public/onsite-be/config/timer.conf failed INFO 08/24/16 02:07:56 salt1be-d1-ap(**5896**/0) main.c(780*****):--operation(): AutoAuthOSUserSubmit. StartOperation***** INFO 08/24/16 02:07:56 salt1be-d1-ap(5896/0) main.c(784):--Client Information: Request from host 'malt-d1-wb' process id 12382. DEBUG 08/24/16 02:07:56 salt1be-d1-ap(5896/0) TOci.cc(571):FetchServiceObjects: ServiceCert.sql DEBUG 08/22/16 23:15:53 pepper1be-d1-ap(2680/0) vsserviceagent.cpp(517):Generate Certificate 2: c1cd00d5c3de082360a08730fef9cd1d DEBUG 08/22/16 23:15:53 pepper1be-d1-ap(2680/0) junk.c(1373):GenerateWebPin : poid: **900105270**, poidLen: 9 DEBUG 08/22/16 23:15:53 pepper1be-d1-ap(2680/0) junk.c(1408):GenerateWebPin : pinStr DEBUG 08/24/16 02:07:56 salt1be-d1-ap(5896/0) uaadapter_vasco_totp.c(275):UAVascoTOTPImpl.close() -- Releasing Adapter Context DEBUG 08/22/16 23:15:53 pepper1be-d1-ap(2680/0) vsenterprise.cpp(288):VSEnterprise::Engage returns 0x4002 - Unknown error code **(0x4002)** ERROR 08/22/16 23:15:53 pepper1be-d1-ap(2680/0) vsautoauth.cpp(696):OSAAEndUserEnroll: error occurred. **Roll back** all updates! INFO 08/24/16 02:07:56 salt1be-d1-ap(5896/0) uaotptokenstoreqmimpl.cpp(199):Close token store INFO 08/24/16 02:07:56 salt1be-d1-ap(5896/0) main.c(990):-- EndOperation -- 08/24 02:07:56 [mds.ecas(5896) ECAS_CP1] **==== END ====** OPERATION = AutoAuthOSUserSubmit, rc = 0x0 (0) SYSINFO Elapse = 0.687, Heap = 1334K, Stack = 64K -- https://mail.python.org/mailman/listinfo/python-list