当前位置:   article > 正文

shell脚本之awk命令_awk 执行shell命令

awk 执行shell命令

1. awk的用法

语法格式:awk [选项] ‘指令’ 操作文件

常用选项:

-F: 指定分隔符,分隔符用""引起来

-v:var=value在awk程序开始之前指定一个值valu给变量var,这些变量值用于awk程序的BEGIN快

-f:后面跟一个保存了awk程序的文件,代替在命令行指定awk程序

2、实例1:在命令行直接输入awk指令

2.1、准备一个1.txt文件,内容如下

 

2.2、逐行读取文件1.txt内容并打印

  命令:awk '{print}' 1.txt 

  1. [dhapp@conch01 shell]$ awk '{print}' 1.txt
  2. 123 abc
  3. 123 cdf
  4. 345 ewr
  5. 12345 dsa
  6. 765 sdfv
  7. [dhapp@conch01 shell]$

 2.3、逐行读取文件内容,并打印该行,$0保存的是当前行的内容

 命令:awk ‘{print $0}’ 1.txt

  1. [dhapp@conch01 shell]$ awk '{print $0}' 1.txt
  2. 123 abc
  3. 123 cdf
  4. 345 ewr
  5. 12345 dsa
  6. 765 sdfv
  7. [dhapp@conch01 shell]$

上面标示awk对字段(列)的提取,如下:

字段提取:提取一个文本中的一列数据并打印输出

字段相关内置变量

  • $0 表示整行文本
  • $1 表示文本行中的第一个数据字段
  • $2 表示文本行中的第二个数据字段
  • $N 表示文本行中的第N个数据字段
  • $NF 表示文本行中的最后一个数据字段
  1. [dhapp@conch01 shell]$ awk '{print $1}' 1.txt
  2. 123
  3. 123
  4. 345
  5. 12345
  6. 765
  7. [dhapp@conch01 shell]$ awk '{print $2}' 1.txt
  8. abc
  9. cdf
  10. ewr
  11. dsa
  12. sdfv
  13. [dhapp@conch01 shell]$

注意:在不指定分割符的情况下,awk默认使用空白做分割符

2.4、为分隔符打印/etc/passwd文件的第一例内容

命令:awk -F ":" '{print $1}' passwd

  1. [dhapp@conch01 shell]$ awk -F ":" '{print $1}' passwd
  2. root
  3. bin
  4. daemon
  5. adm
  6. lp
  7. sync
  8. shutdown
  9. halt
  10. mail
  11. operator
  12. games
  13. ftp
  14. nobody
  15. systemd-network
  16. dbus
  17. polkitd
  18. rpc
  19. rpcuser
  20. nfsnobody
  21. postfix
  22. chrony
  23. sshd
  24. vagrant
  25. clickhouse
  26. dhapp
  27. ntp
  28. [dhapp@conch01 shell]$

3、将awk指令写入文件,通过-f选项调用

3.1、创建一个awkscript文件

  1. [dhapp@conch01 shell]$ vim awkscript
  2. [dhapp@conch01 shell]$ cat awkscript
  3. BEGIN {
  4. FS=":"
  5. }
  6. {print $1}
  7. [dhapp@conch01 shell]$

3.2、通过调用awk指令文件来执行awk命令

命令:awk -f awkscript passwd

  1. [dhapp@conch01 shell]$ awk -f awkscript passwd
  2. root
  3. bin
  4. daemon
  5. adm
  6. lp
  7. sync
  8. shutdown
  9. halt
  10. mail
  11. operator
  12. games
  13. ftp
  14. nobody
  15. systemd-network
  16. dbus
  17. polkitd
  18. rpc
  19. rpcuser
  20. nfsnobody
  21. postfix
  22. chrony
  23. sshd
  24. vagrant
  25. clickhouse
  26. dhapp
  27. ntp
  28. [dhapp@conch01 shell]$

4、awk的BEGIN块和END块

BEGIN用于初始化FS变量(列分隔符),打印标题,或者初始化后需要在程序中调用的全局变量

END用于执行最后的运算或者打印最终的输出结果

END块和BEGIN不是必须的

5、在awk中使用正则匹配,正则表达式必须要放在//中

5.1、打印1.txt中匹配123的那一行内容

命令:awk ‘/123/{print}’ 1.txt

  1. [dhapp@conch01 shell]$ cat 1.txt
  2. 123 abc
  3. 123 cdf
  4. 345 ewr
  5. 12345 dsa
  6. 765 sdfv
  7. [dhapp@conch01 shell]$ awk '/123/{print}' 1.txt
  8. 123 abc
  9. 123 cdf
  10. 12345 dsa
  11. [dhapp@conch01 shell]$

5.2、以" "为分隔符打印1.txt中匹配123的那一行中,第二列的内容

命令:awk -F " " '/123/{print $2}' 1.txt

  1. [dhapp@conch01 shell]$ awk -F " " '/123/{print $2}' 1.txt
  2. abc
  3. cdf
  4. dsa
  5. [dhapp@conch01 shell]$

5.3、打印passwd中,第一列匹配root的行其第二列的内容 ~表示匹配

命令:awk -F ":" '$1 ~ /root/{print $2}' /etc/passwd

  1. [dhapp@conch01 shell]$ awk -F ":" '$1 ~ /root/{print $2}' /etc/passwd
  2. x
  3. [dhapp@conch01 shell]$

6、awk的表达式和块

awk提供了多个比较操作符:"==" “>” “<” “<=” “>=” “!=” "~"匹配 "!~"不匹配

6.1、以":"为分隔符,打印/etc/passwd中第一列是root的行其第3列的内容

命令:awk 'BEGIN { FS=":"} $1 == "root" { print $3 }' passwd

 7、awk中的条件语句

7.1、以":“为分隔符,打印/etc/passwd文件中第一列匹配root的行,其第二列的内容

命令:awk ‘BEGIN {FS=":"} {if ($1 ~ “root”) {print $2}}’ /etc/passwd

7.2、以":"为分隔符,打印1.txt文件中,第一列匹配linux或者第二列匹配Network的行,其第三列的内容

命令:awk 'BEGIN {FS=”:"} ($1 ~ “linux” || $2 ~ “Network”) {print $3}’ 1.txt 

在这里插入图片描述 

7.3、 统计匹配ock的行有多少列

在awk中NF变量记录的是当前行中有多少列(默认是空格为分割符) 

命令:awk ‘/ock/{print NF}’ 1.txt 

在这里插入图片描述

7.4、打印1.txt中有3列的行的内容,示例中,第二行有三列,所以打印的就是第二行的内容

命令:awk ‘NF == 3 {print}’ 1.txt 

在这里插入图片描述

7.5、 输出1.txt中行号大于3的行,其最后一列的内容

$NF记录的是当前行最后一列的内容(如果每一行有多列内容)

NR记录的是当前行的行号

命令:awk ‘{if (NR > 3) {print $NF} }’ 1.txt

在这里插入图片描述 

 

7.6、格式化输入1.txt中行号大于3的行的行号和内容

命令:awk ‘{if (NR > 3) {print NR".\t"$0} }’ 1.txt

在这里插入图片描述

 

7.7、统计1.txt中有多少空行

命令:awk ‘BEGIN { x=0 } /^$/{x=x+1} END{print “find” " " x " " “blank lines”}’ 1.txt 

在这里插入图片描述

 

7.8、将3.txt文件中第二列内容做平方运算后再加1输入(字符串做平方运算后结果为0)

命令:awk ‘{print ($2**2) +1}’ 3.txt

在这里插入图片描述

 8、awk脚本示例

功能:打印1.txt文本中每一列的内容

  1. #!/bin/bash
  2. num=`awk -F ' ' 'NR==1 {print NF}' 1.txt` # 统计1.txt文件有多少列
  3. for i in `seq 1 $num` # 根据文件列数进行循环
  4. do
  5. awk -v a=$i '{print $a}' 1.txt # 打印每一列的内容,-v 参数可以指定一个变量保存外部变量的值,将外部变量传递给awk
  6. done
  1. [dhapp@conch01 shell]$ vim awk.sh
  2. [dhapp@conch01 shell]$ cat 1.txt
  3. 123 abc
  4. 123 cdf
  5. 345 ewr
  6. 12345 dsa
  7. 765 sdfv
  8. [dhapp@conch01 shell]$ sh awk.sh
  9. 123
  10. 123
  11. 345
  12. 12345
  13. 765
  14. abc
  15. cdf
  16. ewr
  17. dsa
  18. sdfv
  19. [dhapp@conch01 shell]$ cat awk.sh
  20. #!/bin/bash
  21. num=`awk -F ' ' 'NR==1 {print NF}' 1.txt` # 统计1.txt文件有多少列
  22. for i in `seq 1 $num` # 根据文件列数进行循环
  23. do
  24. awk -v a=$i '{print $a}' 1.txt # 打印每一列的内容,-v 参数可以指定一个变量保存外部变量的值,将外部变量传递给awk
  25. done
  26. [dhapp@conch01 shell]$

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/304530
推荐阅读
相关标签
  

闽ICP备14008679号