赞
踩
语法格式:awk [选项] ‘指令’ 操作文件
常用选项:
-F: 指定分隔符,分隔符用""引起来
-v:var=value在awk程序开始之前指定一个值valu给变量var,这些变量值用于awk程序的BEGIN快
-f:后面跟一个保存了awk程序的文件,代替在命令行指定awk程序
2.1、准备一个1.txt文件,内容如下
2.2、逐行读取文件1.txt内容并打印
命令:awk '{print}' 1.txt
[dhapp@conch01 shell]$ awk '{print}' 1.txt 123 abc 123 cdf 345 ewr 12345 dsa 765 sdfv [dhapp@conch01 shell]$2.3、逐行读取文件内容,并打印该行,$0保存的是当前行的内容
命令:awk ‘{print $0}’ 1.txt
[dhapp@conch01 shell]$ awk '{print $0}' 1.txt 123 abc 123 cdf 345 ewr 12345 dsa 765 sdfv [dhapp@conch01 shell]$上面标示awk对字段(列)的提取,如下:
字段提取:提取一个文本中的一列数据并打印输出
字段相关内置变量
- $0 表示整行文本
- $1 表示文本行中的第一个数据字段
- $2 表示文本行中的第二个数据字段
- $N 表示文本行中的第N个数据字段
- $NF 表示文本行中的最后一个数据字段
[dhapp@conch01 shell]$ awk '{print $1}' 1.txt 123 123 345 12345 765 [dhapp@conch01 shell]$ awk '{print $2}' 1.txt abc cdf ewr dsa sdfv [dhapp@conch01 shell]$注意:在不指定分割符的情况下,awk默认使用空白做分割符
2.4、为分隔符打印/etc/passwd文件的第一例内容
命令:awk -F ":" '{print $1}' passwd
[dhapp@conch01 shell]$ awk -F ":" '{print $1}' passwd root bin daemon adm lp sync shutdown halt operator games ftp nobody systemd-network dbus polkitd rpc rpcuser nfsnobody postfix chrony sshd vagrant clickhouse dhapp ntp [dhapp@conch01 shell]$
3.1、创建一个awkscript文件
[dhapp@conch01 shell]$ vim awkscript [dhapp@conch01 shell]$ cat awkscript BEGIN { FS=":" } {print $1} [dhapp@conch01 shell]$3.2、通过调用awk指令文件来执行awk命令
命令:awk -f awkscript passwd
[dhapp@conch01 shell]$ awk -f awkscript passwd root bin daemon adm lp sync shutdown halt operator games ftp nobody systemd-network dbus polkitd rpc rpcuser nfsnobody postfix chrony sshd vagrant clickhouse dhapp ntp [dhapp@conch01 shell]$
BEGIN用于初始化FS变量(列分隔符),打印标题,或者初始化后需要在程序中调用的全局变量
END用于执行最后的运算或者打印最终的输出结果
END块和BEGIN不是必须的
5.1、打印1.txt中匹配123的那一行内容
命令:awk ‘/123/{print}’ 1.txt
[dhapp@conch01 shell]$ cat 1.txt 123 abc 123 cdf 345 ewr 12345 dsa 765 sdfv [dhapp@conch01 shell]$ awk '/123/{print}' 1.txt 123 abc 123 cdf 12345 dsa [dhapp@conch01 shell]$5.2、以" "为分隔符打印1.txt中匹配123的那一行中,第二列的内容
命令:awk -F " " '/123/{print $2}' 1.txt
[dhapp@conch01 shell]$ awk -F " " '/123/{print $2}' 1.txt abc cdf dsa [dhapp@conch01 shell]$5.3、打印passwd中,第一列匹配root的行其第二列的内容 ~表示匹配
命令:awk -F ":" '$1 ~ /root/{print $2}' /etc/passwd
[dhapp@conch01 shell]$ awk -F ":" '$1 ~ /root/{print $2}' /etc/passwd x [dhapp@conch01 shell]$
awk提供了多个比较操作符:"==" “>” “<” “<=” “>=” “!=” "~"匹配 "!~"不匹配
6.1、以":"为分隔符,打印/etc/passwd中第一列是root的行其第3列的内容
命令:awk 'BEGIN { FS=":"} $1 == "root" { print $3 }' passwd
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
功能:打印1.txt文本中每一列的内容
#!/bin/bash num=`awk -F ' ' 'NR==1 {print NF}' 1.txt` # 统计1.txt文件有多少列 for i in `seq 1 $num` # 根据文件列数进行循环 do awk -v a=$i '{print $a}' 1.txt # 打印每一列的内容,-v 参数可以指定一个变量保存外部变量的值,将外部变量传递给awk done
[dhapp@conch01 shell]$ vim awk.sh [dhapp@conch01 shell]$ cat 1.txt 123 abc 123 cdf 345 ewr 12345 dsa 765 sdfv [dhapp@conch01 shell]$ sh awk.sh 123 123 345 12345 765 abc cdf ewr dsa sdfv [dhapp@conch01 shell]$ cat awk.sh #!/bin/bash num=`awk -F ' ' 'NR==1 {print NF}' 1.txt` # 统计1.txt文件有多少列 for i in `seq 1 $num` # 根据文件列数进行循环 do awk -v a=$i '{print $a}' 1.txt # 打印每一列的内容,-v 参数可以指定一个变量保存外部变量的值,将外部变量传递给awk done [dhapp@conch01 shell]$
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。