linux大文件讀取和切割


在生產環境中有時候可能會遇到大文件的讀取問題,但是大文件讀取如果按照一般的手法。如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就是對關鍵字的過濾,你可以改成時間段

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM