【Linux命令】awk命令

awk

格式

再次给出awk的基本语法:awk [option] 'pattern{action}' file1,file2,...filen

pattern 指一些匹配动作
如正则表达式,或BEGIN END 等变量

action 指的是指定的动作  
print 等 如 {print $1,$2,$3,"dog"} 

option 指的是 指定内置变量 或 自定义变量等
cat awk_test.txt | awk -v FS='#' '{print $2,$3}' # 内置变量
awk -v var='yeyz' 'BEGIN{print var}'    # 自定义变量

使用 ‘pattern{action}’

[root@dev01 yeyz_shell]# cat awk_test.txt 
this is a test file 
this is a test file 
this is a test file 
this is a test file 
this is a test file 
# 可以看到,当我们把pattern的模式设置问BEGIN或者END的时候,它就可以在我们输出文件的时候,添加文件的首尾字符串
# 需要注意的是,BEGIN和END不能写为begin或者end
# awk '{print $1,$2}'是指打印出这个文件的第一列和第二列。
# 当我们不指定分隔符的时候,awk会默认按照空格来进行分割,当字符中间的空格有多个的时候,awk会将连续的空格理解为一个分隔符
# 当我们使用awk '{print $0}'的时候,会将这一列的值全部打印出来。
# 如果需要拼接字符串的话,只需要在print的后面添加你想要拼接的字符串即可
[root@dev01 yeyz_shell]# cat awk_test.txt | awk 'begin{print "this is awk result:"} {print $1,$2,$3,"dog"} end{print "awk end"}'
this is a dog
this is a dog
this is a dog
this is a dog
this is a dog
[root@dev01 yeyz_shell]# cat awk_test.txt | awk 'BEGIN{print "this is awk result:"} {print $1,$2,$3,"dog"} END{print "awk end"}'
this is awk result:
this is a dog
this is a dog
this is a dog
this is a dog
this is a dog
awk end

使用 option 内置变量

上面例子分别从option、pattern以及action三个方面对awk命令进行了一些介绍,接下来我们看看awk命令当中的有些内置变量,常用的内置变量有:

NR行号,当前处理文本行的行号
NF当前行的字段的个数
FNR各文件分别计数的行号
FILENAME文件名称
FS输入字段分隔符输入的分隔符
OFS输出字段分隔符输出的连接符
ARGV以及ARGC数组以及命令行参数的个数ARGV 命令数组,ARGV[0] 为 awk, ARGC 为命令行参数数量
# 当我们使用awk -F#的时候,awk命令就是用#作为分割符号,来分割这个文件中的内容了。
[root@dev01 yeyz_shell]# cat awk_test.txt 
this#is#a#test##file 
this#is#a#test##file 
this#is#a#test##file 
this#is#a#test##file 
# 相当于指定 option 选项
# -F 指定分隔符  等同于  FS   输入字段分隔符  —— 输入的分割  
[root@dev01 yeyz_shell]# cat awk_test.txt | awk -F# '{print $1,$2}' 
this is
this is
this is
this is

# FS   输入字段分隔符  —— 输入的分割
[root@dev01 yeyz_shell]# cat awk_test.txt 
this#is#a#test##file 
this#is#a#test##file 
this#is#a#test##file 
this#is#a#test##file 
[root@dev01 yeyz_shell]# cat awk_test.txt | awk -v FS='#' '{print $2,$3}'
is a
is a
is a
is a

# OFS  输出字段分隔符 —— 输出的连接
[root@dev01 yeyz_shell]# cat awk_test2.txt 
this is a shell program
this is a shell program
this is a shell program
this is a shell program
[root@dev01 yeyz_shell]# cat awk_test2.txt | awk -v OFS='-' '{print $2,$3,$4}'
is-a-shell
is-a-shell
is-a-shell
is-a-shell

# NR 统计行号 1,2 表示第一行,第二行
# NF 统计每行字段数,第一行有5个字段
# FNR 各文件分别计数的行号 因为支持多个文件   所以有这种情况 awk '{ print NR,$0 }' a.txt b.txt 这样可以每个文件单独计算行数
$ cat a.txt
this is a test program
this is a shell test program
$ cat a.txt | awk '{ print NR,$0 }'
1 this is a test program
2 this is a shell test program
$ cat a.txt | awk '{ print NF,$0 }'
5 this is a test program
6 this is a shell test program

# ARGV[0]指的是awk这个命令,这一点是awk命令规定的,其他的参数都是值得是后面处理的文件的名称,ARGC指的是ARGV数组的值的个数,在本例子中,它的值是3
# 因为 aaa 是我们打印的,不属于输入的命令
$ awk 'BEGIN{print "aaa",ARGV[0],ARGV[1],ARGV[2],ARGC}' test1 test2
aaa awk test1 test2 3

使用 option 自定义变量

以上就是awk的内置变量,如果我们要自定义自己想要的变量,可以通过下面的方式来进行定义:
[root@dev01 yeyz_shell]# awk -v var='yeyz' 'BEGIN{print var}'
yeyz
[root@dev01 yeyz_shell]# awk  'BEGIN{ var2="yyy" ; print var2}'
yyy
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值