2019.9.17 awk上课总结

sed
[root@localhost ~]# head -5 /etc/passwd | sed -nr 's#.*:/(.)(.*/){2,}.*#1#p'
r
b
s //sed查找先找到对应行,然后把需要过滤的( )起来,其他再慢慢过滤。
v
v
--------------------------------------------
[root@localhost ~]# ifconfig ens33 | sed '1,2c\t111' //合并第一二行并tab键在下面插入111
111
inet6 fe80::c893:d365:c5bd:752e prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:eb:90:6d txqueuelen 1000 (Ethernet)
--------------------------------------------------------------------------------------
cat data|awk '{sum+=$1} END {print "Sum = ", sum}' //求和
cat data|awk '{sum+=$1} END {print "Average = ", sum/NR}' //求平均值
cat data|awk 'BEGIN {max = 0} {if ($1>max) max=$1 } END {print "Max=", max}' //求最大值
awk 'BEGIN {min = 1999999} {if ($1<min) min=$1} END {print "Min=", min}' //求最小值

awk
[root@localhost ~]# awk -F':' '{print $1,$2,$4}' /etc/passwd
tcpdump x 72
123456 x 1000 //awk处理的文件没有空格的情况下需要-F指定分隔符
dhcpd x 177
named x 25
--------------------------------------------------
[root@localhost ~]# cat ip.sh
inet 192.168.200.111 netmask 255.255.255.0 broadcast 192.168.200.255
[root@localhost ~]# awk '{print $2}' ip.sh
192.168.200.111
[root@localhost ~]# awk '{ip=$2; print ip}' ip.sh //awk可以引用变量
192.168.200.111
---------------------------------------------------
[root@localhost ~]# awk 'BEGIN{print "Device IP ---------"} {print "ens33 "$2} END{print "crushllinux"}' ip.sh
Device IP
---------
ens33 192.168.200.111 //区块的执行流程,
crushllinux
--------------------------------------------------------
[root@localhost ~]# awk '{print $1 " " $3}' ip.sh
inet netmask //需要打印的东西要用""阔起来 是tab键

------------------------------------------------------------------------------------
[root@localhost ~]# awk '/[Gg]reen/' hua.sh
xiong.xiong 05/99 48311 Green 8 40 44 //过滤出来Green或者green
J.Luluv 06/99 48317 green 9 24 26
----------------------------------------------------------------------
[root@localhost ~]# awk '/^.*a/{print $0}' hua.sh
asgadag 05/99 4712 Brown-2 12 30 28 //过滤包含a的
---------------------------------------------------------------------------
[root@localhost ~]# awk -F':' '{print NF}' /etc/passwd //已:作为分割符,显示列数,有空格的话就默认空格作为分隔符。
[root@localhost ~]# awk -F':' '{print $(NF)}' /etc/passwd //过滤最后一行
[root@localhost ~]# awk -F':' '{print $(NF-1)}' /etc/passwd //过滤最后第二行
------------------------------------------------------------------------------------------
[root@localhost ~]# df | awk -F'[ %]' '//$/{print $(NF-2)}'
11 //用awk过滤出来已经用的11%内存
----------------------------------------------------------------------------
[root@localhost ~]# awk 'END{print NR}' /etc/passwd
45 //查看行数
------------------------------------------------------------------
[root@localhost ~]# awk 'END{print NR,FILENAME}' /etc/passwd
45 /etc/passwd //awk浏览的文件名
----------------------------------------------------------------------------
[root@localhost ~]# awk '{print NR,$0}' 9999.sh
1 aa bb:cc //显示行号并且显示全文
2 11 22:33
3 22 33:44
----------------------------------------------------------------------------------
[root@localhost ~]# awk 'NR==1,NR==3{print NF,NR,$0}' hua.sh
7 1 Miong.xiong 05/99 48311 Green 8 40 44
7 2 J.Luluv 06/99 48317 green 9 24 26 //显示1到3行的
7 3 P.bunny 02/99 48 Yellow 12 35 26
-----------------------------------------------------------------------------------
[root@localhost ~]# awk 'NR%2==1{print NR,$0}' hua.sh
1 Miong.xiong 05/99 48311 Green 8 40 44
3 P.bunny 02/99 48 Yellow 12 35 26 //awk取奇数行
5 Lsgadag 05/99 4712 Brown-2 12 30 28
----------------------------------------------------------------------------------------
[root@localhost ~]# [root@localhost ~]# free | awk 'NR==2{int($3/$2*100)> 50 ;print "baojing"}'
baojing //超过报警
-----------------------------------------------------------------------------------------
[root@localhost ~]# name="li si li kun"
[root@localhost ~]# awk -v xx="$name" 'BEGIN{print xx}'
li si li kun //awk引用变量
-------------------------------------------------------------------------------
[root@localhost ~]# awk -F':' '{sum+=$3}END{print sum}' /etc/passwd //以下全部是sum+=的用法
79455
[root@localhost ~]# awk 'BEGIN{FS=":"} {sum+=$3}END{print sum}' /etc/passwd
79455
[root@localhost ~]# awk 'BEGIN{FS=":"} NR==1,NR==2 {sum+=$3}END{print sum}' /etc/passwd
1
[root@localhost ~]# awk 'BEGIN{FS=":"} NR==1,NR==5 {sum+=$3}END{print sum}' /etc/passwd
10
[root@localhost ~]# awk 'BEGIN{FS=":"} NR==1 || NR==5 {sum+=$3}END{print sum}' /etc/passwd
4
[root@localhost ~]# awk 'BEGIN{FS=":"} NR==2 || NR==5 {sum+=$3}END{print sum}' /etc/passwd
5
--------------------------------------------------------------------------------------------
[root@localhost ~]# df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/mapper/centos-root 38770180 4226644 34543536 11% /
devtmpfs 917592 0 917592 0% /dev
tmpfs 933512 0 933512 0% /dev/shm
tmpfs 933512 20952 912560 3% /run
tmpfs 933512 0 933512 0% /sys/fs/cgroup
/dev/sda1 1038336 182364 855972 18% /boot
tmpfs 186704 8 186696 1% /run/user/42
tmpfs 186704 28 186676 1% /run/user/0
/dev/sr0 4414592 4414592 0 100% /run/media/root/CentOS 7 x86_64
1k-块的奇数跟已用的偶数相加
[root@localhost ~]# df | sed '1d' | awk '{if(NR%2==1)print NR,NF,$2 ;else print NR,NF,$3}'
1 6 38770180
2 6 0
3 6 933512
4 6 20952
5 6 933512
6 6 182364
7 6 186704
8 6 28
9 8 4414592
[root@localhost ~]# df | sed '1d' | awk '{if(NR%2==1)print NR,NF,$2 ;else print NR,NF,$3}'|awk '{sum+=$3} END{print sum}'
45441844
-------------------------------------------------------------------------------------------------------------------------------------------
[root@localhost ~]# nl hua.sh //awk引用if判断
1 Miong.xiong 05/99 48311 Green 8 40 44
2 J.Luluv 06/99 48317 green 9 24 26
3 P.bunny 02/99 48 Yellow 12 35 26
4 J.troll 07/99 4842 Brown-3 12 26 26
5 Lsgadag 05/99 4712 Brown-2 12 30 28

[root@localhost ~]# awk '{if($NF==26)print "小的"; else if($NF==28)print "大的";else print "巨大"}' hua.sh 巨大
小的
小的
小的
大的
巨大
----------------------------------------------------------------------------------------------------------------
[root@localhost ~]# cat ip.sh | grep -Ev '^$|='| wc -l
13 //E是支持正则表达式,可以同时处理两个问题

-------------------------------------------------------------------------------------------
[root@localhost ~]# cat access_log | awk '{print $1}'| sort | uniq -c | sort -nr | head
//过滤出来的文件先排序,再去除重复数,然后在-nr以大小排序,最后查看前10个
---------------------------------------------------------------------------------------------
[root@localhost ~]# cat ip.sh | xargs -n 2
2222 3344 //把竖的变成横的排序
12 463
[root@localhost ~]# cat ip.sh | paste -d ' ' - -
2222 3344
12 463
[root@localhost ~]# sed 'N; s/ / /' ip.sh
2222 3344
12 463
------------------------------------------------------------------------------------
[root@localhost abc]# rename txt mp4 {1..10}.txt
[root@localhost abc]# ls
10.mp4 1.mp4 2.mp4 3.mp4 4.mp4 5.mp4 6.mp4 7.mp4 8.mp4 9.mp4

[root@localhost abc]# ls | sed -r 's/(.*)(.mp4)/mv 12 1.txt /g' | bash
[root@localhost abc]# ls
10.txt 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt

[root@localhost abc]# ls | awk -F. '{print "mv "$1".txt "$1".mp4"}'|bash
10.mp4 1.mp4 2.mp4 3.mp4 4.mp4 5.mp4 6.mp4 7.mp4 8.mp4 9.mp4
---------------------------------------------------------------------------------------------------

原文地址:https://www.cnblogs.com/otherwise/p/11543245.html