在生產環境中有時候可能會遇到大文件的讀取問題,但是大文件讀取如果按照一般的手法。如cat這種都是對io的一個挑戰,如果io扛得住還好,如果扛不住
造成的后果,如服務器內存奔潰,日志損壞
方法一:
sed
例子: 按照你自己的日志格式 sed -n '/14\/Mar\/2015:21/,/14\/Mar\/2015:22/p' access.log >/home/test/test.log
sed -n "1,1000p" access.log >/home/test/test.log
新生成的test.log就是那個時間段的
方法二:
linux split命令
split -l 1000000 access.log -d -a 10 acclog_
方法三:
類似python的第三方工具
word='abc' with open('test.txt','r',encoding='utf-8') as f: #test.txt為你的源文件 with open('test2.txt','w',encoding='utf-8') as f2: #test2.txt為你新生成的包含關鍵字的文件 for line in f: if word in line: f2.write(line) 這里地word就是對關鍵字的過濾,你可以改成時間段