我有一個日誌文件,我想刪除一些特定的部分。下面顯示了日誌文件的一部分:如何刪除包含特定字符串但字符串內部長度不同的字符串?
I0216 10:18:04.720626 31559 solver.cpp:273] Solving
I0216 10:18:04.720630 31559 solver.cpp:274] Learning Rate Policy: step
I0216 10:18:05.242708 31559 solver.cpp:219] Iteration 0 (0 iter/s, 0.522037s/50 iters), loss = 1.60944
I0216 10:18:05.242750 31559 solver.cpp:238] Train net output #0: accuracy = 0
I0216 10:18:05.242763 31559 solver.cpp:238] Train net output #1: loss = 1.60944 (* 1 = 1.60944 loss)
I0216 10:18:05.242785 31559 sgd_solver.cpp:105] Iteration 0, lr = 1e-10
I0216 10:18:22.386440 31559 solver.cpp:219] Iteration 50 (2.91648 iter/s, 17.144s/50 iters), loss = 1.60944
I0216 10:18:22.386497 31559 solver.cpp:238] Train net output #0: accuracy = 0.643982
I0216 10:18:22.386509 31559 solver.cpp:238] Train net output #1: loss = 1.60944 (* 1 = 1.60944 loss)
I0216 10:18:22.386515 31559 sgd_solver.cpp:105] Iteration 50, lr = 1e-10
I0216 10:18:39.549926 31559 solver.cpp:219] Iteration 100 (2.91313 iter/s, 17.1637s/50 iters), loss = 1.60944
I0216 10:18:39.550071 31559 solver.cpp:238] Train net output #0: accuracy = 1
I0216 10:18:39.550087 31559 solver.cpp:238] Train net output #1: loss = 1.60944 (* 1 = 1.60944 loss)
I0216 10:18:39.550093 31559 sgd_solver.cpp:105] Iteration 100, lr = 1e-10
I0216 10:18:56.714752 31559 solver.cpp:219] Iteration 150 (2.91292 iter/s, 17.1649s/50 iters), loss = 1.60944
I0216 10:18:56.714824 31559 solver.cpp:238] Train net output #0: accuracy = 0.624222
I0216 10:18:56.714838 31559 solver.cpp:238] Train net output #1: loss = 1.60944 (* 1 = 1.60944 loss)
I0216 10:18:56.714845 31559 sgd_solver.cpp:105] Iteration 150, lr = 1e-10
I0216 10:19:13.893241 31559 solver.cpp:219] Iteration 200 (2.91059 iter/s, 17.1787s/50 iters), loss = 1.60944
I0216 10:19:13.893450 31559 solver.cpp:238] Train net output #0: accuracy = 1
I0216 10:19:13.893467 31559 solver.cpp:238] Train net output #1: loss = 1.60944 (* 1 = 1.60944 loss)
I0216 10:19:13.893473 31559 sgd_solver.cpp:105] Iteration 200, lr = 1e-10
I0216 10:19:31.094591 31559 solver.cpp:219] Iteration 250 (2.90674 iter/s, 17.2014s/50 iters), loss = 1.60944
I0216 10:19:31.094650 31559 solver.cpp:238] Train net output #0: accuracy = 0.61937
I0216 10:19:31.094662 31559 solver.cpp:238] Train net output #1: loss = 1.60944 (* 1 = 1.60944 loss)
I0216 10:19:31.094667 31559 sgd_solver.cpp:105] Iteration 250, lr = 1e-10
I0216 10:19:48.290045 31559 solver.cpp:219] Iteration 300 (2.90772 iter/s, 17.1956s/50 iters), loss = 1.60944
I0216 10:19:48.290187 31559 solver.cpp:238] Train net output #0: accuracy = 0.959229
I0216 10:19:48.290205 31559 solver.cpp:238] Train net output #1: loss = 1.60944 (* 1 = 1.60944 loss)
I0216 10:19:48.290210 31559 sgd_solver.cpp:105] Iteration 300, lr = 1e-10
I0216 10:20:05.504201 31559 solver.cpp:219] Iteration 350 (2.90457 iter/s, 17.2142s/50 iters), loss = 1.60944
I0216 10:20:05.504257 31559 solver.cpp:238] Train net output #0: accuracy = 0.772217
I0216 10:20:05.504271 31559 solver.cpp:238] Train net output #1: loss = 1.60944 (* 1 = 1.60944 loss)
如可以看出,有一些與31559 solver.cpp:219] Iteration
我想開始,在不改變文件的其它行線,只有改變這些線,例如,這樣一句:FROM
... solver.cpp:219] Iteration 14750 (2.9004 iter/s, 17.239s/50 iters), loss = 1.60934
要
... solver.cpp:219] Iteration 14750, loss = 1.60934
.
.
.
這意味着我想從包含上述行的行刪除子串(2.9004 iter/s, 17.239s/50 iters)
,但其他行保持不變。 謝謝
我想刪除包含(2.8995 iter/s, 17.2444s/50 iters)
的行中的那些部分,這個字符串的長度可能互不相同。這部分與(
開始和以數量繼續(其可以是從其他行不同的,並與iter/s,
繼續和重新編號和與iters)
結束
AS @ delca85建議的圖案是這樣的:
p = "(\(\d*[.]?\d* iter/s\,\s\d*[.]?\d*)(s/[0-9]+)?(\siters\))"
有沒有人有一個建議?在此先感謝
感謝您的回覆,我如何打開文件並找到'p =「\([0-9 \。] + iter/s \,[0-9 \。] + s/[0-9] + iters \)「'並從文件中刪除字符串。不應該讀程序?謝謝 –
@ S.EB我在文件中添加了替換行。我希望這可以最終幫助你,你會接受和upvote我的答案。 – delca85
感謝您的回答。不幸的是,它不起作用,因爲你帶來的'string'在行中不一樣。迭代數字不斷變化 –