我有一個包含大量文本的日誌文件,其中有些文件是無用的。在這個日誌中有一些對我很重要的行。這些行的模式是:需要解析bash中的日誌文件
0x00000001 (NEEDED) Shared library: [libm.so.6]
0x00000001 (NEEDED) Shared library: [libc.so.6]
0x00000001 (NEEDED) Shared library: [ld.so.1]
0x00000001 (NEEDED) Shared library: [libgcc_s.so.1]
需要的關鍵字可以在對我很重要的所有行上找到。 []之間的關鍵字對我來說很重要。我需要創建所有這些字符串的列表,而不必重複它們。
我已經在Python上做了這個,但看起來像在機器上我想運行腳本沒有Python可用,所以我需要在bash中重寫腳本。我只知道bash中的基本內容,而且我無法爲我的問題找到解決方案。
我使用的Python腳本是:
import sys
import re
def testForKeyword(keyword, line):
findStuff = re.compile(r"\b%s\b" % keyword, \
flags=re.IGNORECASE)
if findStuff.search(line):
return True
else:
return False
# Get filename argument
if len(sys.argv) != 2:
print("USAGE: python libraryParser.py <log_file.log>")
sys.exit(-1)
file = open(sys.argv[1], "r")
sharedLibraries = []
for line in file:
if testForKeyword("NEEDED", line):
libraryNameStart = line.find("[") + 1
libraryNameFinish = line.find("]")
libraryName = line[libraryNameStart:libraryNameFinish]
# No duplicates, only add if it does not exist
try:
sharedLibraries.index(libraryName)
except ValueError:
sharedLibraries.append(libraryName)
for library in sharedLibraries:
print(library)
能否請你幫我解決這個問題? 在此先感謝。
一個解決辦法是拉出包含所有行 「需要」 的使用'grep',然後使用'cut'將方括號中的行拆分,然後使用'uniq'來刪除所有重複項。 –
你想要什麼? .so的列表? – Marcus
你在Python中的「算法」是什麼樣的?您需要展示一些幫助我們的工作。考慮一下你的python代碼中發生了哪些步驟。然後像「這裏是我在python中執行的步驟」這樣的問題,1. .... 2 .... 3 .... shell中的等效或最佳實踐技巧是什麼?會表明你不只是尋找一些免費諮詢。 (編輯你的問題,不要在評論中回覆; - )。祝你好運。 – shellter