Monday, November 2, 2020

Working with log files in Bash shell


1: To stream a log file $ tail -f 2020-11-02_1604333192.log It will print anything that is being appended to the log file. 2: Print last 10 lines of the log file. $ tail -10 2020-11-02_1604333192.log 3: Stream a log file but also apply a string based filter to print only "ERROR" or "INFO" logs. $ tail -f 2020-11-02_1604333192.log | grep INFO $ tail -f 2020-11-02_1604333192.log | grep ERROR 4: Print the entire log file as per its present state. $ cat 2020-11-02_1604333192.log 5: Print the log file as per its present state but also filter it to show only 'ERROR' logs. $ cat 2020-11-02_1604333192.log | grep ERROR 6: Print the number of lines in the log file. $ wc -l 2020-11-02_1604333192.log 1130 2020-11-02_1604333192.log 7: Print the number of lines representing 'ERROR' (or 'INFO') logs. $ cat 2020-11-02_1604333192.log | grep ERROR | wc -l 87 $ cat 2020-11-02_1604333192.log | grep INFO | wc -l 1086 $ grep ERROR 2020-11-02_1604333192.log | wc -l 87 8: If you want to track how many lines have been added in the log file every one second or two seconds, use the following 'Python 3' code in the Python shell (or CLI). Note: 'time.sleep(1)' puts Python into sleep for one second. Ashish Jain@LAPTOP MINGW64 /e/ws/logs $ python Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32 >>> import time >>> import os >>> while True: ... time.sleep(1) ... os.system('wc -l 2020-11-21_1605972090.log') ... 982 2020-11-21_1605972090.log 0 984 2020-11-21_1605972090.log 0 989 2020-11-21_1605972090.log 0 And so on...

No comments:

Post a Comment