Shell study Note 3: Deep script Training

Source: Internet
Author: User
Tags egrep

I have been learning Shell recently. I used to think that Shell syntax is very difficult and hard to learn. Now I have some gains. In fact, Shell scripts are a collection of linux commands. You can understand what the commands in each step of the script mean, and then the whole script means. You also need to complete a job through the script, we can break this work into several small commands, and then we can combine them. As for awk, sed, and regular expressions, we must rely on our usual accumulation.

① Query the number of requests of a service on a certain day:

cat renren-1.log|grep "MSG without parse">tempawk '{a[$1]++}END{for(i in a)print i,a[i]}' temp

② The first column is the same, and the second column is added:

awk '{a[$1]+=$2}END{for(m in a)print m,a[m]}'

③ Query the name of a file containing log characters:

find . -name "*.log*" -type f | xargs ls -l

④ Query the number of Batch Service requests per day:

#! /Bin/bash # query the number of requests of Batch Service in a specific day. find. -name "* log *"-type f | xargs ls-l> logs_llawk '{print $8} 'logs_ll> logscat logs | while read linedo echo $ line echo 2013-08-01 cat $ line | egrep ">>> | MSG without parse" | grep "2013-08-01" | wc-ldone

⑤ Query the number of requests of Batch Service on a specific day and filter out requests with 0 requests:

#! /Bin/bash # query the number of requests of a batch service on a specific day, and filter out the find requests whose number of requests is 0. -name "* log *"-type f | xargs ls-l> logs_llawk '{print $8} 'logs_ll> logscat logs | while read lineCOUNT = 'cat $ line | egrep" >>>| MSG without parse "| grep" 2013-07-02 "| wc-l 'do if [$ COUNT-ne 0]; then echo $ line 2013-07-02 echo $ COUNT else echo $ line>/dev/null fidone

6. query the number of requests of the batch service for a specific month and filter out the requests whose number of requests is 0:

#! /Bin/bash # query the number of requests of a batch service in a month, and filter out the find requests whose number of requests is 0. -name "* log *"-type f | xargs ls-l> logs_llawk '{print $8} 'logs_ll> logsfor day in 'seq 30' do cat logs | while read line COUNT = 'cat $ line | egrep ">>| MSG without parse" | grep "2013-08-$ day" | wc-l' do if [$ COUNT-ne 0]; then echo $ line 2013-08-$ day echo $ COUNT else echo $ line>/dev/null fi donedone

7. count the number of requests for a service in a certain period of time and the corresponding date:

cat $line | egrep ">>> |MSG without parse" |awk '{print $1}' >>log.tmpcat log.tmp |sort |uniq -c

⑧ Queries the number of requests of the batch service on all dates, filters out requests with 0 requests, and outputs the service name, date, and number of corresponding requests:

#! /Bin/bash # query the number of requests of the batch service on all dates, filter out requests with 0 requests, and output the service name, date, and number of corresponding requests to find. -name "* log *"-type f | xargs ls-l> logs_llawk '{print $6, $8} 'logs_ll> logsawk '{print $2}' logs> logcat log | while read linedo cat $ line | egrep ">>>| MSG without parse" | awk '{ print $1} '> $ line. tmp echo $ line cat $ line. tmp | sort | uniq-cdone


This article from the "Rainbow cat sword walking Tianya" blog, please be sure to keep this source http://ailurus.blog.51cto.com/4814469/1300406

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.