awk指示简述
参考文献:https://blog.csdn.net/u010502101/article/details/81839519
AWK, 统计数据过滤器辅助工具 (近似于grep,比grep强悍),属
文件格式
文件格式1:后置指示 | awk [快捷键] ‘前提{撰稿指示}’
文件格式2:awk [快捷键] ‘前提{撰稿指示}’ 文件…
撰稿指示假如包涵数条句子时,能用王劝隔开,处置文档时,若隐脉助记符,则预设将字符、CSV等做为助记符。print是最常用的指示。
快捷键
-F:选定助记符,可略去(预设字符或Tab位)
-V:初始化内部Shell表达式 variable
1.统计数据总线段表达式
awk把拆分后的统计数据总线段手动重新分配给统计数据总线段表达式
$0则表示aes文档
$1则表示文档行中第二个统计数据总线段
$2则表示文档行中第二个统计数据总线段
$n则表示文档行中第n个统计数据总线段
$ cat test.txt
The dog:There is a big dog and a little dog inthe park
The cat:There is a big cat and a little catinthe park
The tiger:There is a big tiger and a litle tiger inthe park
通过快捷键-F选定“:”为字段助记符,把每行统计数据分为两段,然后输出第三个统计数据总线段$2。
$ awk -F: {print $2}test.txt
There is a big dog and a little doginthe park
There is a big cat and a little cat inthe park
There is a big tiger and a litle tiger inthe park
注意:如不显示选定字段助记符,awk的预设字段助记符为任意空白字符,包括CSV、字符符、换行符等。
2.在脚本中使用多个指示
$ awk -F: {$1=”Description:”; print $0}test.txt
Description: There is a big dog and a little dog inthe park
Description: There is a big cat and a little catinthe park
Description: There is a big tiger and a litle tiger inthe park
3.从文件中读程序命令
$ vim pokes.txt
{
$1=“Description:”print $0}
$ awk -F: -f pokes.txt test.txt
Description: There is a big dog and a little doginthe park
Description: There is a big cat and a little cat inthe park
Description: There is a big tiger and a litle tigerinthe park
4.在处置统计数据之前运行脚本
awk预设每次读入一行统计数据,然后用脚本进行处置。假如想在处置文档之前预处置一些指示,能用BEGIN关键字选定。
$ awk -F: BEGIN{print “开始处置…”}{print $2}test.txt
开始处置…
There is a big dog and a little doginthe park
There is a big cat and a little cat inthe park
There is a big tiger and a litle tigerinthe park
5.在处置统计数据后运行脚本
用END关键字在处置完所有统计数据后,再运行善后处置工作。
$ awk -F: {print $2} END{print “处置结束…”}test.txt
There is a big dog and a little dog inthe park
There is a big cat and a little cat inthe park
There is a big tiger and a litle tigerinthe park
处置结束…
6.在program中使用表达式
表达式又分为两种形式:awk内置的表达式;用户自定义的表达式。
内置表达式与记录助记符相关表达式
FS :输入字段助记符
OFS:输出字段助记符
RS:输入记录拆分符
ORS:输出字段助记符
FIELDWIDTHS:定义统计数据总线段的宽度
FS用法$ cat test.txt
The dog:There is a big dog and a little doginthe park
The cat:There is a big cat and a little cat inthe park
The tiger:There is a big tiger and a litle tigerinthe park
$ awk BEGIN{FS=”:”} {print $1, $2} test.txt #用FS选定字段助记符为“:”,然后用“:”把每行统计数据拆分为两段。The dog There is a big dog and a little dog inthe park
The cat There is a big cat and a little catinthe park
The tiger There is a big tiger and a litle tiger inthe park
OFS用法
用FS选定输入字段助记符“:”后,每行统计数据分为两个统计数据段,输出时,用OFS选定两个统计数据总线段用“>”拼接。
$ cat test.txt
The dog:There is a big dog and a little doginthe park
The cat:There is a big cat and a little cat inthe park
The tiger:There is a big tiger and a litle tigerinthe park
$ awk BEGIN{FS=”:”; OFS=”>”} {print $1, $2} test.txt #其实就是,FS选定字段助记符为“:”,然后将选定的助记符替换为>The dog>There is a big dog and a little dog inthe park
The cat>There is a big cat and a little catinthe park
The tiger>There is a big tiger and a litle tiger inthe park
RS和ORS用法预设情况下RS和ORS设置为“\n”,则表示输入数据流中的每一行做为一条记录,输出时每条记录之间也以“\n”进行拆分。
下面以a.txt文件为例,a.txt文件中内容如下:
$ cat a.txt
Tom is a student
and he is 20 years old
Bob is a teacher
and he is 40 years old
预设情况下,每行做为一条记录处置,但此种情况下,要把第一行和第二行做为一条记录处置,第三行和第四行做为一条记录处置。
$ awk BEGIN{RS=””; ORS=”\n”; FS=”and”; OFS=”,”} {print $1, $2}a.txt
Tom is a student
, he is 20 years old
Bob is a teacher
, he is 40 years old
\n把前两行、后两行各看作一条记录来处置,然后把选定助记符and替换为逗号。
常用字符截取
提取IP地址先查出IP地址
$ ip add
1: lo:
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0:
link/ether 00:15:5d:0b:f5:03 brd ff:ff:ff:ff:ff:ff
inet 10.5.6.244/24 brd 10.5.6.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::b268:c536:3f01:4c85/64 scope link noprefixroute
valid_lft forever preferred_lft forever
先把这一行拉出来
$ ip add | grep global
inet 10.5.6.244/24 brd 10.5.6.255 scope global noprefixroute eth0
##再把以字符为隔开的第二列拉出来$ ip add | grep global | awk{print $2}10.5.6.244/24
##然后把以/24为助记符的第一列拉出来$ ip add | grep global | awk {print $2}| awk -F/24{print $1} # 以/24为助记符的第一列10.5.6.244
这样就OK了。
假如需要提取广播,提取第四列
$ ip add | grep global | awk {print $4}10.5.6.255
提取home目录可用容量
$ df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 904M 0 904M 0% /dev
tmpfs 915M 0 915M 0% /dev/shm
tmpfs 915M 8.5M 907M 1% /run
tmpfs 915M 0 915M 0% /sys/fs/cgroup
/dev/mapper/centos-root 50G 2.0G 48G 4% /
/dev/sda1 1014M 180M 835M 18% /boot
/dev/mapper/centos-home 74G 33M 74G 1% /home
tmpfs 183M 0 183M 0% /run/user/0
$ df -h | grep home
/dev/mapper/centos-home 74G 33M 74G 1% /home
$ df -h | grep home | awk {print $4}74G