In operation and maintenance work, we should analyze the log of back-Office system frequently, and provide the basic data for the monitoring result by grasping the key word information in the log and counting the fetch results.
The following shell demonstrates how to get the desired statistic from a large number of logs. It shows a variety of interesting commands and the use of the shell.
The specific function is to count the number of occurrences of a keyword at different times. The result is formatted as "HH Times"
#!/bin/sh# gets the date in YYYYMMDD format to determine the range of log files to crawl current_date= ' date ' +%y%m%d ' '; # stitching up the log file name to be crawled file_name= $current _date "* All* "; # finds a keyword in a batch of log files and forms a temporary file for finding results. # The result format of the temporary file is: File name: Log content #./pmclog/20171020_all_000000.log:00:00:00.876652| D|38|0327|get| Caccumulate.cpp|delete maingrep ' Delete main './pmclog/$file _name >> term_del.log;# Get the time period information from the temp file, This information is the first two characters of the original log content and saves the results to a temporary file # If you need additional information, you need to modify AWK's command parameters. Awk-f ': ' {print $} ' term_del.log >> term_del_hour.log;rm term_del.log;rm term_del_hour_times.log;# get current period hour = ' Date ' +%h "'; # use a loop to count every time of day for ((i=0; i<= $hour; i++)) do # as a number, the front of the variable i is no 0, here to format a zero, 0 points is" 00 ", This can match the information in the temporary file hour2=$ (printf '%02d ' $i); # with the combination of Grep-o and wc-l, you can count the number of occurrences of a particular character in a file. # using Echo in front can be used to mark the number of times each time the data # is the final Term_del_hour_times.log is the final result file. echo $hour 2 ' grep-o $hour 2 term_del_hour.log | wc-l ' >> TERM_DEL_HOUR_TIMES.LOG;DONE;RM term_del_hour.log;
Use the shell in Linux to analyze the information in the statistics log