AWK's use---business needs

Source: Internet
Author: User

awk is handled by streams, so it is still possible to process 1-5g text data!


Specify the log format

$17 to be DomainName

$19 to be Request

$21 In response to the status code

App 1: Match statistics F5 Log, containing the number of a domain name


The following methods can be used to set

[Email protected] scripts]# awk-f: ' $1== ' root ' {print $} '/etc/passwd | Wc-l1


For the above revision, because the statistics when the use of wc-l, now do not need to wc-l, awk complete statistics

[[email protected] scripts]# awk-f:-V n=0 ' {if ($1== "root") n++;} End{print n} '/etc/passwd 1


-V can specify a variable, which is used directly when using variables in awk, without the need to "$n" this place to differentiate the shell


NOTE: The following statement, by default, does not specify the n variable, although the result is because the default setting of n is 0 when n++, but this will cause a bug, when there is no match, go greater than n when the time is not 0 but empty

[[email protected] scripts]# awk-f: ' {if ($1== "root") n++;} End{print n} '/etc/passwd 1


Bug

[[email protected] scripts]# awk-f: ' {if ($1== "Rooot") n++;} End{print n} '/etc/passwd [[email protected] scripts]#



Application 2: Using variables in the shell and defining multivariable and logical operators

&& Logic and

~ Match String

-V key1=value1-v key2-values



Prototype:

Awk-v t=0-vdomain= "$domain"-v request= "/main/detail"-v code=500 ' $17==domain && $19 ~ request&& $21 = =code {t++} end{print t} ' Access.log

0


Test statement:

[Email protected] scripts]# awk-f: ' $1== ' root && $3==0 {print $} '/etc/passwdroot:x:0:0:root:/root:/bin/bash



Attention:

To determine the matching method:

1) $n -Regular Expression

2) if ($n ~ regular expression ) print $


If you use the BEGIN statement in awk, be sure to use if the pattern match is not used or the error

Such as:

Error:

[[email protected] scripts]# awk-f: ' $1== ' root ' && $3==0 begin{n=0} {n++} end{print n} '/etc/passwd

Awk: $1== "root" && $3==0 begin{n=0} {t++} End{print t}

awk: ^ Syntax error



Switch

[[email protected] scripts]# awk-f: ' begin{n=0} {if ($ = = "root" && $3==0) n++} end{print n} '/etc/passwd1




Apply an array in the 3:awk

3 of the specified domain name

[[email protected] f5-log]$ awk ' $17 = = ' gold.dianpingfang.com ' {++domain[$21]} END {for (kin domain) print k,domain[k]} ' AC Cess.log

2

200 4498

301 2

500 15

302 321

304 2


This article is from the "Tridewah operation and maintenance work Road" blog, please be sure to keep this source http://cuidehua.blog.51cto.com/5449828/1771599

AWK's use---business needs

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.