从0到精通超详细SHELL正则三剑客-AWK

发布时间:2024年01月07日

AWK是什么

awk是一个强大的linux命令,有强大的文本格式化能力,好比将一些文本数据格式化成专业的excel表的样式。

awk更是一门编程语言,支持条件判断,数组,循环等功能。

学awk就两件事,行,列

  • 对行操作,对列操作

    • 如何分割数据

    • 如何输出数据,指定第一行,到第三行

awk模式,动作

模式,是指,要操作那些行

动作,是指,找到这些行之后,干什么,如何处理

生成测试数据
[root@service ~]# echo cc{01..50} | xargs -n 5
cc01 cc02 cc03 cc04 cc05
cc06 cc07 cc08 cc09 cc10
cc11 cc12 cc13 cc14 cc15
cc16 cc17 cc18 cc19 cc20
cc21 cc22 cc23 cc24 cc25
cc26 cc27 cc28 cc29 cc30
cc31 cc32 cc33 cc34 cc35
cc36 cc37 cc38 cc39 cc40
cc41 cc42 cc43 cc44 cc45
cc46 cc47 cc48 cc49 cc50
[root@service ~]# echo cc{01..50} | xargs -n 5 > wyx.log
无模式,只有动作

不写模式,默认处理每一行。

1.直接输出源文件内容
$0是表示列的数据,默认是表示一整行数据
[root@service ~]# awk '{print $0}' wyx.log
cc01 cc02 cc03 cc04 cc05
cc06 cc07 cc08 cc09 cc10
cc11 cc12 cc13 cc14 cc15
cc16 cc17 cc18 cc19 cc20
cc21 cc22 cc23 cc24 cc25
cc26 cc27 cc28 cc29 cc30
cc31 cc32 cc33 cc34 cc35
cc36 cc37 cc38 cc39 cc40
cc41 cc42 cc43 cc44 cc45
cc46 cc47 cc48 cc49 cc50
2.输出第一列数据
[root@service ~]# awk '{print $1}' wyx.log
cc01
cc06
cc11
cc16
cc21
cc26
cc31
cc36
cc41
cc46
3.输出第一列和第三列的数据
[root@service ~]# awk '{print $1,$3}' wyx.log
cc01 cc03
cc06 cc08
cc11 cc13
cc16 cc18
cc21 cc23
cc26 cc28
cc31 cc33
cc36 cc38
cc41 cc43
cc46 cc48
行变量NR,匹配范围语法
NR 行
NR== 等于行
NR>= 大于等于行
NR<= 小于等于
NR>=N&&NR<=M 从N行到M行
1.打印第二行数据
[root@service ~]# awk 'NR==2{print $0}' wyx.log
cc06 cc07 cc08 cc09 cc10
2.打印第二行,第一列和第四列数据
[root@service ~]# awk 'NR==2{print $1,$4}' wyx.log
cc06 cc09
3.在开头显示行号
[root@service ~]# awk '{print NR,$0}' wyx.log
4.在结尾显示行号
[root@service ~]# awk '{print $0,NR}' wyx.log
列变量NF,每一列的字段
输出列:
,	输出分隔符,默认逗号表示空格
$0   输出所有字段
$1   输出第一列
$N   输出第N列
$NF  输出最后一列
$(NF-1)	输出倒数第二列
1.查看每一行有多少字段
[root@service ~]# awk '{print $0,NF}' wyx.log
cc01 cc02 cc03 cc04 cc05 5
cc06 cc07 cc08 cc09 cc10 5
cc11 cc12 cc13 cc14 cc15 5
cc16 cc17 cc18 cc19 cc20 5
cc21 cc22 cc23 cc24 cc25 5
cc26 cc27 cc28 cc29 cc30 5
cc31 cc32 cc33 cc34 cc35 5
cc36 cc37 cc38 cc39 cc40 5
cc41 cc42 cc43 cc44 cc45 5
cc46 cc47 cc48 cc49 cc50 5
2.输出最后一列
[root@service ~]# awk '{print $NF}' wyx.log
cc05
cc10
cc15
cc20
cc25
cc30
cc35
cc40
cc45
cc50
3.输出倒数第二列
[root@service ~]# awk '{print $(NF-1)}' wyx.log
cc04
cc09
cc14
cc19
cc24
cc29
cc34
cc39
cc44
cc49
NR行记录,行数据,awk处理的第几行
NF字段的数量,表示这一行数据分了几列
$1第一个字段的数据,$2,$3以此类推
FS数据输入的字段分割符,默认是空格
RS行分割符,默认是换行符
测试数据
[root@service ~]# awk -v RS=' ' '{print $0}' wyx.log
cc01
cc02
cc03
cc04
cc05
cc06
cc07
cc08
cc09
cc10
cc11
cc12
cc13
cc14
cc15
cc16
cc17
cc18
cc19
cc20
cc21
cc22
cc23
cc24
cc25
cc26
cc27
cc28
cc29
cc30
cc31
cc32
cc33
cc34
cc35
cc36
cc37
cc38
cc39
cc40
cc41
cc42
cc43
cc44
cc45
cc46
cc47
cc48
cc49
cc50
[root@service ~]# awk -v ORS='@@' '{print $0}' wyx.log
cc01 cc02 cc03 cc04 cc05@@cc06 cc07 cc08 cc09 cc10@@cc11 cc12 cc13 cc14 cc15@@cc16 cc17 cc18 cc19 cc20@@cc21 cc22 cc23 cc24 cc25@@cc26 cc27 cc28 cc29 cc30@@cc31 cc32 cc33 cc34 cc35@@cc36 cc37 cc38 cc39 cc40@@cc41 cc42 cc43 cc44 cc45@@cc46 cc47 cc48 cc49 cc50@@

面试题解析

测试数据:
[root@service ~]# vim test1.log
Theres no doubt that my mother gives all her love to me. I do believe she is a great person who makes my life beautiful and meaningful.  She is an easygoing and kind woman with bright eyes and a lovely smile. Although she is often busy, I still feel that I am taken good care of by her. Its a great pleasure to chat with her when I get into troubles. She always encourages me not to give up and tries to cheer me up by coming up with good solutions. In addition, I am fascinated by her cooking and writing.  With her love, I feel like a fish swimming happily in a beautiful sea. Ill cherish her love forever.
1.统计单词出现的频率,并且统计出现最多的前五个(awk答题)
[root@service ~]# awk -v RS=' ' '{print $0}' test1.log | sort | uniq -c | sort -r | head -5
      6 I
      5 her
      5 and
      5 a
      4 to
2.tr命令解答
[root@service ~]# cat test1.log | tr ' ' '\n' | sort | uniq -c | sort -r | head -5
      6 I
      5 her
      5 and
      5 a
      4 to
3.sed解答
[root@service ~]# sed -r 's/[^a-zA-Z]+/\n/g' test1.log | sort | uniq -c | sort -r | head -5
      6 I
      6 her
      5 and
      5 a
      4 to
4.grep解答
[root@service ~]# grep -Eo '[a-zA-Z]+' test1.log  | sort | uniq -c | sort -r | head -5
      6 I
      6 her
      5 and
      5 a
      4 to

文章来源:https://blog.csdn.net/m0_66011019/article/details/135415728
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。