awk 命令

awk 命令

简介:

​ awk其名称得自于它的创始人 Alfred Aho 、Peter Weinberger 和 Brian Kernighan 姓氏的首个字母。实际上 AWK 的确拥有自己的语言: AWK 程序设计语言 , 三位创建者已将它正式定义为“样式扫描和处理语言”。它允许您创建简短的程序,这些程序读取输入文件、为数据排序、处理数据、对输入执行计算以及生成报表,还有无数其他的功能。

​ awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大。简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理。awk有3个不同版本: awk、nawk和gawk,未作特别说明,一般指gawk,gawk 是 AWK 的 GNU 版本。

使用语法:
awk  参数 'BEGIN{} // {action1;action2} END{}'  file_name    # awk里面变量不用加$  
可选的参数:
  • -v 定义变量
  • -f 调用脚本
  • -F 指定分隔符

​ 尽管操作可能会很复杂,但语法总是这样,其中 BEGIN 表示 在开始之前做的事, // 是匹配代码块,可以是字符串或正则表达式,用斜杠包起来。而 action 是在找到匹配内容时所执行的一系列命令,包含一条或多条命令,多条命令用 ; 隔开。END{} 是结尾代码块,在对每一行进行处理之后再执行的代码块,主要是进行最终计算或输出结尾摘要信息

​ awk语言的最基本功能是在文件或者字符串中基于指定规则浏览和抽取信息,awk抽取信息后,才能进行其他文本操作。完整的awk脚本通常用来格式化文本文件中的信息。通常,awk是以文件的一行为处理单位的。awk每接收文件的一行,然后执行相应的命令,来处理文本。

awk中字符的含义:

$0      表示全部内容
$1      每行第一个字段
NF      字段数量
NR      每行的记录号,多文件记录递增
FNR     与NR类似,不过多文件记录不递增,每个文件都从1开始
\t      制表符,tab键一个位置
\n      换行符
FS      BEGIN时定义分隔符
RS      输入的记录分隔符, 默认为换行符(即文本是按一行一行输入)
~       包含
!~      不包含
==      等于,必须全部相等,精确比较
!=      不等于,精确比较
&&      逻辑与
||      逻辑或
-F [:#/]    定义了三个分隔符
+       匹配时表示1个或1个以上
OFS     输出字段分隔符, 默认也是空格,可以改为其他的
ORS     输出的记录分隔符,默认为换行符,即处理结果也是一行一行输出到屏幕
/[0-9][0-9]+/     两个或两个以上数字
/[0-9][0-9]*/     一个或一个以上数字
调用awk:
1.将所有的awk命令插入一个单独文件,然后调用:
awk -f awk-script-file input-file(s)
其中,-f选项加载awk-script-file中的awk脚本,input-file(s)跟上面的是一样的。

2.命令行方式
awk [-F  field-separator]  'commands'  input-file(s)
其中,commands 是真正awk命令,[-F域分隔符]是可选的。 input-file(s) 是待处理的文件。
在awk中,文件的每一行中,由域分隔符分开的每一项称为一个域。通常,在不指名-F域分隔符的情况下,默认的域分隔符是空格。

3.shell脚本方式
将所有的awk命令插入一个文件,并使awk程序可执行,然后awk命令解释器作为脚本的首行,一遍通过键入脚本名称来调用。
相当于shell脚本首行的:#!/bin/sh
可以换成:#!/bin/awk
print和printf

​ awk中同时提供了print和printf两种打印输出的函数。

​ 其中print函数的参数可以是变量、数值或者字符串。字符串必须用双引号引用,参数用逗号分隔。如果没有逗号,参数就串联在一起而无法区分。这里,逗号的作用与输出文件的分隔符的作用是一样的,只是后者是空格而已。

printf函数,其用法和c语言中printf基本相似,可以格式化字符串,输出复杂时,printf更加好用,代码更易懂。

简单使用awk:

打印全部内容

# 原文件
[root@localhost ~]# cat tese 
This is the first line
This is the second line
This is the third line
This is the fourth line

# 使用 awk 打印
[root@localhost ~]# awk '{print $0}' tese 
This is the first line
This is the second line
This is the third line
This is the fourth line

打印第一列和第三列

[root@localhost ~]# awk '{print $1}' tese 
This
This
This
This

[root@localhost ~]# awk '{print $4}' tese 
first
second
third
fourth

打印第三行第四列的内容

# 原文件
[root@localhost ~]# cat  tese 
This is the first line
This is the second line
This is the third line
This is the fourth line

# 处理后
[root@localhost ~]# awk 'NR==3{print $4}' tese 
third

打印倒数第二列

[root@localhost ~]# cat tese 
This is the third line
ThIS is the first line
This is the second line
This is the third line
This is the fourth line
ThIS is the first line

[root@localhost ~]# awk '{print $(NF-1)}' tese 
third
first
second
third
fourth
first

输出文本行数

[root@localhost ~]# cat tese   # 原文件 
This is the third line
This is the first line
This is the second line
This is the third line
This is the fourth line
This is the first line

[root@localhost ~]# awk 'BEGIN{i=0}{i++;}END{print i}' tese    # 将i定义成变量,自加,打印i 
6

分隔符

[root@localhost ~]# cat tese   # 原文件
This is the third line
This is the first line
This is the second line
This is the third line
This is the fourth line
This is the first line

[root@localhost ~]# awk -F " " '{print $2,$4}' tese  # 以空格为分隔符,打印$2,$4
is third
is first
is second
is third
is fourth
is first

打印\t

[root@localhost ~]# cat tese   # 空格处为tab键的一个位置
This    is the third line
This    is the first line
This    is the second line
This    is the third line
This    is the fourth line
This    is the first line

[root@localhost ~]# awk -F "\t" '{print $2}' tese 
is the third line
is the first line
is the second line
is the third line
is the fourth line
is the first line

匹配This,打印第四列

[root@localhost ~]# cat tese  # 原文件
This is the third line
This is the first line
This is the second line
This is the third line
This is the fourth line
This is the first line

[root@localhost ~]# awk '/This/{print $4}' tese 
third
first
second
third
fourth
first

位置调换

[root@localhost ~]# echo "546-654-985 zhangsan nan"|awk '{print $2,$3,$1}'
zhangsan nan 546-654-985

计算

[root@localhost ~]# awk 'BEGIN{print 30+20}'
50

定义分隔符为is,打印第一列和三列

[root@localhost ~]# cat tese 
This is the third line
This is the first line
This is the second line
This is the third line
This is the fourth line
This is the first line

[root@localhost ~]# awk 'BEGIN{FS="is"}{print $1,$3}' tese 
Th  the third line
Th  the first line
Th  the second line
Th  the third line
Th  the fourth line
Th  the first line

包含第四列为 first ,打印包含的第一列

[root@localhost ~]# cat tese 
This is the third line
ThIS is the first line
This is the second line
This is the third line
This is the fourth line
ThIS is the first line

[root@localhost ~]# awk '$4 ~ /first/ {print $1}' tese 
ThIS
ThIS

以 is 为分隔符,输出第一列和第三列和以 - 输出显示

[root@localhost ~]# cat tese 
This is the third line
ThIS is the first line
This is the second line
This is the third line
This is the fourth line
ThIS is the first line

[root@localhost ~]# awk 'BEGIN{FS="is";OFS="-"}{print $1,$2}' tese 
Th- 
ThIS - the first line
Th- 
Th- 
Th- 
ThIS - the first line

awk的内容极多,这里只罗列简单的用法。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值