awk 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

awk 命令安装:


  1. -bash/zsh: awk command not found
  2.  
  3. #Debian
  4. apt-get install gawk
  5.  
  6. #Ubuntu
  7. apt-get install gawk
  8.  
  9. #Alpine
  10. apk add gawk
  11.  
  12. #Arch Linux
  13. pacman -S gawk
  14.  
  15. #Kali Linux
  16. apt-get install gawk
  17.  
  18. #CentOS
  19. yum install gawk
  20.  
  21. #Fedora
  22. dnf install gawk
  23.  
  24. #OS X
  25. brew install awk
  26.  
  27. #Raspbian
  28. apt-get install gawk
  29.  
  30. #Docker
  31. docker run cmd.cat/awk awk

awk 命令说明:


awk 命令补充说明:


AWK 是另一种流行的流编辑器,类似于 SED。 awk 的基本功能是在文件中搜索包含一种或多种模式的行或其他文本单元。 当一行与其中一个模式匹配时,会对该行执行特殊操作。

有几种方法可以运行 awk。 如果程序很短,最简单的方法是在命令行上运行它:

  1. awk PROGRAM inputfile(s)

如果必须对多个文件进行多次更改,可能定期对多个文件进行更改,则将 awk 命令放在脚本中会更容易。 这是这样读的:

  1. awk -f PROGRAM-FILE inputfile(s)

awk 中最常用的程序是 print,我们很快就会看到。awk 中的打印命令从输入文件中输出选定的数据。变量 $1, $2, $3, ..., $N 保存输入行的第一个、第二个、第三个直到最后一个字段的值。 变量 $0(零)保存整行的值。

如上图:要打印大小 $2 和使用% $5,请使用 df -h | awk '{print $2,$5}'注意$2,$5:它们之间的 , 将用 空格 分隔输出。

  1. $ df -h | awk '{print $2,$5}'
  2.  
  3. -------------------
  4. Size Use%
  5. 40G 25%
  6. 7.8G 0%
  7. 7.8G 0%
  8. 7.8G 1%
  9. 7.8G 0%
  10. 1000G 12%
  11. 1.6G 0%
  12. 1.6G 0%

awk 命令语法:


  1. awk [-F sepstring] [-v assignment]... program [argument...]
  2.  
  3. awk [-F sepstring] -f progfile [-f progfile]... [-v assignment]...[argument...]

awk 命令选项:


  1. -F sepstring
  2. Define the input field separator. This option shall be
  3. equivalent to:
  4.  
  5. -v FS=sepstring
  6.  
  7. except that if -F sepstring and -v FS=sepstring are
  8. both used, it is unspecified whether the FS assignment
  9. resulting from -F sepstring is processed in command
  10. line order or is processed after the last -v
  11. FS=sepstring. See the description of the FS built-in
  12. variable, and how it is used, in the EXTENDED
  13. DESCRIPTION section.
  14.  
  15. -f progfile
  16. Specify the pathname of the file progfile containing an
  17. awk program. A pathname of '-' shall denote the
  18. standard input. If multiple instances of this option
  19. are specified, the concatenation of the files specified
  20. as progfile in the order specified shall be the awk
  21. program. The awk program can alternatively be specified
  22. in the command line as a single argument.
  23.  
  24. -v assignment
  25. The application shall ensure that the assignment
  26. argument is in the same form as an assignment operand.
  27. The specified variable assignment shall occur prior to
  28. executing the awk program, including the actions
  29. associated with BEGIN patterns (if any). Multiple
  30. occurrences of this option can be specified.

awk 命令实例:


awk 在空格分隔的文件中打印第五列(又名字段):

  1. awk '{print $5}' filename

awk 在空格分隔的文件中打印包含“某物”的行的第二列:

  1. awk '/something/ {print $2}' filename

awk 打印文件中每一行的最后一列,使用逗号(而不是空格)作为字段分隔符:

  1. awk -F ',' '{print $NF}' filename

awk 对文件第一列中的值求和并打印总数:

  1. awk '{s+=$1} END {print s}' filename

awk 对第一列中的值求和并漂亮地打印这些值,然后是总数:

  1. awk '{s+=$1; print $1} END {print "--------"; print s}' filename

awk 从第一行开始每三行打印一次:

  1. awk 'NR%3==1' filename

awk Formatting 格式化字段输出:

  1. $ df -h | sort -rnk 5 | head -3 | awk '{ print "Partition " $6 "\t: " $5 " full!" }'
  2.  
  3. Partition /mnt/sdb6 : 99% full!
  4. Partition /mnt/sdb5 : 97% full!
  5. Partition /home : 89% full!

awk / gawk 字符串格式化:


Sequence 含义
\a Bell character
\n Newline character
\t Tab

awk 打印命令和正则表达式,例如,从 /etc 目录列出以字母 ac 开头并以 .conf 结尾的文件并打印第 9 个字段:

  1. $ ls -l /etc/ | awk '/\<(a|c).*\.conf$/ { print $9 }'
  2.  
  3. adduser.conf
  4. apg.conf
  5. ca-certificates.conf
  6. casper.conf
  7. resolv.conf

awk 脚本方式,我们使用 BEGINEND,正则表达式配置到 test.awk 中,然后运行:

  1. BEGIN { print "*** WARNING WARNING WARNING ***" }
  2. /\<[8|9][0-9]%/ { print "Partition " $6 "\t: " $5 " full!" }
  3. END { print "*** Give money for new disks URGENTLY! ***" }
  4.  
  5.  
  6. $ df -h | awk -f test.awk
  7.  
  8. *** WARNING WARNING WARNING ***
  9. Partition / : 89% full!
  10. Partition /mnt/sdb5 : 97% full!
  11. Partition /mnt/sdb6 : 99% full!
  12. Partition /home : 89% full!
  13. *** Give money for new disks URGENTLY! ***

awk 的 RSORSFSOFSNRNF

  1. RSRecord Separator,记录分隔符
  2.  
  3. ORSOutput Record Separate,输出当前记录分隔符
  4.  
  5. FSField Separator,字段分隔符
  6.  
  7. OFSOut of Field Separator,输出字段分隔符
  8.  
  9. NRNumber of Records 当前行数
  10.  
  11. NFNumber of Fields 字段数量

假设 test.txt 文件有 100 行,awk 只取第 20 到第 30 行内容:

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

awk 扩展阅读:




awk 命令评论

共收录到 518Linux 命令