Brief introduction
awk is a powerful text analysis tool, especially when dealing with relatively good log file format, awk is to put it simply reads the file line by line, with a space for the default delimiter (You can also specify delimiters) Each slice processing line.
grammar
awk [选项参数] 'script' var=value file(s)
或
awk [选项参数] -f scriptfile var=value file(s)
awk workflow
awk workflow is such that: reading there is a record newline separated '\ n', and then recorded in the specified field delimiter into domain, fill-in fields, $ 0 indicates all domains is the entire record (this and regular match out similar), $ 1 represents the first field, $ n denotes the n-th field. The default field separator is a space, of course, can be specified delimiter.
Options Parameter Description:
-F fs or --field-separator fs
指定输入文件折分隔符,fs是一个字符串或者是一个正则表达式,如-F:。
-f scripfile or --file scriptfile
从脚本文件中读取awk命令
-v var=value or --asign var=value
赋值一个用户定义变量。
Analytical: -F specified sheet separator colon each row, the output of the first slice and the penultimate intermediate fragments separated by commas
//假设我们要输出supervisor这个程序的进程号 ,这个命令配合xargs可以方便于结束某些进程
ps -ef|grep 'supervisor'|awk '{print $2}'
//输出
1630
比如有如下文件ip.txt
1 134.102.173.43
2 134.102.173.43
3 134.102.171.42
4 134.102.170.9
要统计出现次数最多的IP可以利用以下shell脚本:
cat ip.txt | awk '{print $2}' | sort | uniq -c | sort -n -r | head -n 1
最后如果不加head这一组可以统计从多到少的列表
Resolution:
awk '{print $ 5}': Low 5 domain of access to data (column 5)
sort: IP part of the sort.
uniq -c: print the number of each duplicate rows appear. (And remove duplicate rows)
sort -n -r: arranged in descending order of occurrence of duplicate rows.
head -n 5: Take the top five IP
Built-in variables
ARGC 命令行参数个数
ARGV 命令行参数排列
ENVIRON 支持队列中系统环境变量的使用
FILENAME awk浏览的文件名
FNR 浏览文件的记录数
FS 设置输入域分隔符,等价于命令行 -F选项
NF 浏览的当前行中分片的个数
NR 已读的记录数
OFS 输出域分隔符
ORS 输出记录分隔符
RS 控制记录分隔符
Is used NF, if {print $ (NF-1)} is the penultimate printing fragment
Hereinafter, for purposes of example, we save the / etc / passwd file into demo.txt.
root:x:0:0:root:/root:/usr/bin/zsh
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
Example:
`` `
$ awk -F ':' '{print $1, $(NF-1)}' demo.txt
## 函数
awk还提供了一些内置函数,方便对原始数据的处理。
toupper (): for the characters to uppercase.
tolower (): lowercase characters.
length (): returns the length of the string.
substr (): returns the substrings.
sin (): sinusoidal.
cos (): cosine.
sqrt (): square root.
rand (): random number.
示例:
$ awk -F ':' '{ print toupper($1) }' demo.txt
## 条件表达式
awk允许指定输出条件,只输出符合条件的行。
Only the output of the line containing usr.
$ awk -F ':' '/usr/ {print $1}' demo.txt
Odd line output
$ awk -F ':' 'NR % 2 == 1 {print $1}' demo.txt
root
bin
sync
The output of the third row after row
$ awk -F ':' 'NR >3 {print $1}' demo.txt
sys
sync
The following example of an output value equal to the specified first field line
$ awk -F ':' '$1 == "root" {print $1}' demo.txt
root
$ awk -F ':' '$1 == "root" || $1 == "bin" {print $1}' demo.txt
root
bin
## if 语句
awk提供了if结构,用于编写复杂的条件,if结构还可以指定else部分
The output of the first field of the first character of the row is greater than m.
$ awk -F ':' '{if ($1 > "m") print $1}' demo.txt
root
sys
sync
It includes else's
$ awk -F ':' '{if ($1 > "m") print $1; else print "---"}' demo.txt
root
---
---
sys
sync
```
Some examples of this article comes from the teacher Liao Xuefeng article