In the Linux production environment, there are the following such a common operations need to work.
Every once in a while, such as two hours, the log file to generate the log per second (this is assumed to be testfile.out, the absolute path is/home/panlm/shellpra/ Testfile.out) is an operation that prints the lines of the log containing certain strings separately and puts them back in a file (where the file is assumed to be out.log). These strings that need to be matched can be represented in this way by "0x216000ab" where AB is a continuous integer of 01 to 18.
There are two main steps to achieving such a requirement
One, through the shell script, the implementation of the target log to meet the requirements of the line output to a new file.
Ii. deploying the script through crontab run every two hours
The contents of Testfile.out are as follows
1231 0x21600001
32343 0x21600002
23123 0x21600003
42232 0x21600004
22323jkshdkfkadlfdsd
FJSDKDJF 0x21600018
SDAFD 0x21600043
Sajdkfjjjjls
First, the Shell script implementation
Script name is get.sh
#!/bin/bash
#define VARs
Log_file= "/home/panlm/shellpra/testfile.out
Sum=1
#将字符串 "0x216000ab" is divided into two parts, a fixed part of the string, part of a variable integer part
str1= "0x216000"
Str2= "0x2160000"
For i in {1..18}
Do
If ["$sum"-lt 10]
Then
#将两个字符串合并成一个字符串
Str=${str2}${sum}
In #将满足结果的行数输出到结果文件out. log
grep $str $Log _file |cat >> out.log
Let "sum= $sum +1"
Else
Str=${str1}${sum}
In #将满足结果的行数输出到结果文件out. log
grep $str $Log _file |cat >> out.log
Let "sum= $sum +1"
Fi
Done
After executing the script, the file Out.log contents are as follows
1231 0x21600001
32343 0x21600002
23123 0x21600003
42232 0x21600004
FJSDKDJF 0x21600018
Second, every two hours to execute the script
0 */2 * * */home/panlm/shellpra/get.sh
This article is from the "Love Technology, Love life" blog, please be sure to keep this source http://youmiao.blog.51cto.com/6833914/1569181
Shell Script---View the number of rows in the log file that contain some strings every few hours