Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9154345
  • 博文数量: 1727
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 19860
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1727)

文章存档

2024年(3)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: 其他平台

2021-05-24 11:45:29


点击(此处)折叠或打开

  1. https://www.cnblogs.com/ginvip/p/6352157.html
  2. AWK 为三位创始人的缩写.
  3. AWK 程序设计语言, 定义为“样式扫描和处理语言”。它允许您创建简短的程序,这些程序读取输入文件、为数据排序、处理数据、对输入执行计算以及生成报表,还有无数其他的功能。

  4. awk 是一种很棒的语言,它适合文本处理和报表生成,其语法较为常见,借鉴了某些语言的一些精华,如 C 语言等。

  5. 使用方法
  6. awk '{pattern + action}' {filenames}

  7. pattern 表示 AWK 在数据中查找的内容,pattern就是要表示的正则表达式,用斜杠括起来。
  8. action 是在找到匹配内容时所执行的一系列命令。
  9. {}不需要在程序中始终出现,但它们用于根据特定的模式对一系列指令进行分组。
  10. 花括号用于将几块代码组合到一起

  11. awk语言的最基本功能是在文件或者字符串中基于指定规则浏览和抽取信息,awk抽取信息后,才能进行其他文本操作。完整的awk脚本通常用来格式化文本文件中的信息。

  12. 通常,awk是以文件的一行为处理单位的。awk每接收文件的一行,然后执行相应的命令,来处理文本

点击(此处)折叠或打开

  1. # awk -F":" '{print $0}' /etc/passwd
  2. $0 为一整行  root:x:0:0:root:/root:/bin/bash
  3. $1 为 root  $2为x  FS:字段分隔符  NF为字段数量 NR 为记录(行数)的数量.
  4. -F参数:指定分隔符,可指定一个或多个


点击(此处)折叠或打开

  1. # awk '{if(NR>=20 && NR<=30) print $1}' test.txt

  2. 只查看test.txt文件(100行)内第20到第30行的内容


  3. # awk -F '[ ,]+' '{print $3" "$7}' test.txt
  4. 设定多个分隔符, 然后打印 并以空格分隔.


点击(此处)折叠或打开

  1. BEGIN 和 END 模块

  2. 对于每个输入行, awk 都会执行每个脚本代码块一次
  3. awk 在开始处理输入文件之前会执行 BEGIN 块,因此它是初始化 FS(字段分隔符)变量、打印页眉或初始化其它在程序中以后会引用的全局变量的极佳位置。

  4. awk 在处理了输入文件中的所有行之后执行 END块, END 块用于执行最终计算或打印应该出现在输出流结尾的摘要信息。

  5. # awk 'BEGIN {count=0;print "[start] user count is ",count} {count=count+1;print $0} END{print "[end] user count is ",count}' passwd
  6. 统计/etc/passwd的账户人数, count 为自定义变量.
  7.  
  8. # ll |awk 'BEGIN {size=0;} {size=size+$5;} END{print "[end]size is ",size}'
  9. 统计某个文件夹下的文件占用的字节数



点击(此处)折叠或打开

  1. awk 赋值运算符:a+5;等价于: a=a+5;其他同类
  2. # awk 'BEGIN{a=5;a+=5;print a}'

  3. # awk 'BEGIN{a=1;b=2;print (a>2&&b>1,a=1||b>1)}'
  4. awk逻辑运算符, 返回 0 1

  5. # awk 'BEGIN{a="100testaa";if(a~/100/) {print "ok"}}'
  6. 返回 ok a变量 ~匹配 包含/100/.
  7. # echo|awk 'BEGIN{a="100testaaa"}a~/test/{print "ok"}'
  8. 返回ok


  9. 关系运算符:
  10. 如: > < 可以作为字符串比较,也可以用作数值比较,关键看操作数如果是字符串就会转换为字符串比较。两个都为数字 才转为数值比较。字符串比较:按照ascii码顺序比较。

  11. awk 算术运算符:
  12. 说明,所有用作算术运算符进行操作,操作数自动转为数值,所有非数值都变为0。


  13. 三目运算符 ?:
  14. # awk 'BEGIN{a="b";print a=="b"?"ok":"err"}'
  15. ok



点击(此处)折叠或打开

  1. 字段分隔符 FS
  2. FS="\t" 一个或多个 Tab 分隔

  3. FS="[[:space:]+]" 一个或多个空白空格, 为默认情况
  4. FS="[" ":]+" 以一个或多个空格或:分隔


  5. 字段数量 NF
  6. # awk -F ":" 'NF==8{print $0}' hello.txt


  7. 记录数量 NR
  8. # ifconfig eth0|awk -F [" ":]+ 'NR==2{print $4}' ## NR==2也就是取第2行


  9. RS 记录分隔符变量.
  10. FS 设置成"\n"告诉 awk 每个字段都占据一行。通过将 RS 设置成"",还会告诉 awk每个地址记录都由空白行分隔


  11. [root@Gin scripts]# cat recode.txt
  12. Jimmy the Weasel
  13. 100 Pleasant Drive
  14. San Francisco,CA 123456
  15.  
  16. Big Tony
  17. 200 Incognito Ave.
  18. Suburbia,WA 64890
  19. [root@Gin scripts]# cat awk.txt ## 代码块
  20. #!/bin/awk
  21. BEGIN {
  22.         FS="\n"
  23.         RS=""
  24. }
  25. {
  26.         print $1","$2","$3
  27. }
  28. [root@Gin scripts]# awk -f awk.txt recode.txt
  29. Jimmy the Weasel,100 Pleasant Drive,San Francisco,CA 123456
  30. Big Tony,200 Incognito Ave.,Suburbia,WA 64890


  31. OFS 输出字段分隔符
  32. [root@Gin scripts]# cat hello.txt
  33. root:x:0:0:root:/root:/bin/bash
  34. bin:x:1:1:bin:/bin:/sbin/nologin:888
  35. [root@Gin scripts]# awk 'BEGIN{FS=":"}{print $1","$2","$3}' hello.txt
  36. root,x,0
  37. bin,x,1
  38. [root@Gin scripts]# awk 'BEGIN{FS=":";OFS="#"}{print $1,$2,$3}' hello.txt
  39. root#x#0
  40. bin#x#1


  41. ORS 输出记录分隔符
  42. [root@Gin scripts]# cat recode.txt
  43. Jimmy the Weasel
  44. 100 Pleasant Drive
  45. San Francisco,CA 123456
  46.  
  47. Big Tony
  48. 200 Incognito Ave.
  49. Suburbia,WA 64890
  50. [root@Gin scripts]# cat awk.txt
  51. #!/bin/awk
  52. BEGIN {
  53.         FS="\n"
  54.         RS=""
  55.         ORS="\n\n"
  56. }
  57. {
  58.         print $1","$2","$3
  59. }
  60. [root@Gin scripts]# awk -f awk.txt recode.txt
  61. Jimmy the Weasel,100 Pleasant Drive,San Francisco,CA 123456
  62.  
  63. Big Tony,200 Incognito Ave.,Suburbia,WA 64890


点击(此处)折叠或打开

  1. 正则应用
  2. 规则表达式

  3. awk '/REG/{action} ' file

  4. /REG/为正则表达式,可以将$0 中,满足条件的记录送入到:action 进行处理

  5. # awk '/root/{print $0}' passwd ##匹配所有包含root的行

  6. # awk -F: '$5~/root/{print $0}' passwd ## 以分号作为分隔符,匹配第5个字段是root的行

  7. # ifconfig eth0|awk 'BEGIN{FS="[[:space:]:]+"} NR==2{print $4}'



  8. 布尔表达式
  9. awk '布尔表达式{action}' file 仅当对前面的布尔表达式求值为真时, awk 才执行代码块。
  10. # awk -F: '($1=="root")&&($5=="root") {print $0}' passwd

  11. awk 的 if、循环和数组

  12. {
  13.         if ($1=="foo"){
  14.                 if($2=="foo"){
  15.                         print "uno"
  16.                 }else{
  17.                         print "one"
  18.                 }
  19.         }elseif($1=="bar"){
  20.                 print "two"
  21.         }else{
  22.                 print "three"
  23.         }
  24. }


  25. 同样的 ! /matchme/ { print $1 $3 $4 } 可以转换为
  26. {
  27.   if ( $0 !~ /matchme/ ) {
  28.     print $1 $3 $4
  29.   }
  30. }


  31. 循环结构 do while.

  32.   {
  33.     count=1
  34.     do {
  35.         print "I get printed at least once no matter what"
  36.     } while ( count !=1 )
  37.   }


  38. for 循环
  39. for ( initial assignment; comparison; increment ) {
  40.     code block
  41. }
  42. 例如
  43. for ( x=1;x<=4;x++ ) {
  44.     print "iteration", x
  45. }



  46. break 和 continue
  47. #break 语句示例
  48. x=1
  49. while(1) {
  50.   print "iteration", x
  51.   if ( x==10 ) {
  52.     break
  53.   }
  54.   x++
  55. }

  56. x=1
  57. while (1) {
  58.         if ( x==4 ) {
  59.         x++
  60.         continue
  61.     }
  62.     print "iteration", x
  63.     if ( x>20 ) {
  64.         break
  65.     }
  66.     x++
  67. }


  68. 数组
  69. AWK 中的数组都是关联数组,数字索引也会转变为字符串索引

  70. {
  71.     cities[1]=”beijing”
  72.     cities[2]=”shanghai”
  73.     cities[“three”]=”guangzhou”
  74.     for( c in cities) {
  75.         print cities[c]
  76.     }
  77.     print cities[1]
  78.     print cities[“1”]
  79.     print cities[“three”]
  80. }
  81. for…in 输出,因为数组是关联数组,默认是无序的。所以通过 for…in 得到是无序的数组


  82. 数组的典型应用
  83. 用 awk 中查看服务器连接状态并汇总
  84. netstat -an|awk '/^tcp/{++s[$NF]}END{for(a in s)print a,s[a]}'
  85. ESTABLISHED 1
  86. LISTEN 20

  87. 或者
  88. awk '{a[$7]+=$10;++b[$7];total+=$10}END{for(x in a)print b[x],x,a[x]|"sort -rn -k1";print
  89. "total size is :"total}' /app/log/access_log
  90. total size is :172230
  91. 21 /icons/poweredby.png 83076
  92. 14 / 70546
  93. 8 /icons/apache_pb.gif 18608
  94. a[$7]+=$10 表示以第 7 列为下标的数组( $10 列为$7 列的大小),把他们大小累加得到
  95. $7 每次访问的大小,后面的 for 循环有个取巧的地方, a 和 b 数组的下标相同,所以一
  96. for 语句足矣


点击(此处)折叠或打开

  1. 替换

  2. awk 'BEGIN{info="this is a test2010test!";gsub(/[0-9]+/,"!",info);print info}' this is a
  3. 在 info 中查找满足正则表达式, /[0-9]+/ 用”!”替换,并且替换后的值,赋值给 info 未
  4. 给 info 值,默认是$0

  5. 查找
  6. awk 'BEGIN{info="this is a test2010test!";print index(info,"test")?"ok":"no found";}'
  7. ok #未找到,返回 0

  8. 匹配查找
  9. awk 'BEGIN{info="this is a test2010test!";print match(info,/[0-9]+/)?"ok":"no found";}'
  10. ok #如果查找到数字则匹配成功返回 ok,否则失败,返回未找到

  11. 截取
  12. awk 'BEGIN{info="this is a test2010test!";print substr(info,4,10);}'
  13. s is a tes #从第 4 个 字符开始,截取 10 个长度字符串

  14. 分割
  15. awk 'BEGIN{info="this is a test";split(info,tA," ");print length(tA);for(k in tA){print k,tA[k];}}'
  16. 4 4 test 1 this 2 is 3 a
  17. #分割 info,动态创建数组 tA,awk for …in 循环,是一个无序的循环。 并不是从数组下标 1…n 开始



阅读(1218) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~