Shell Script---View the number of rows in the log file that contain some strings every few hours

Source: Internet
Author: User


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.