0
点赞
收藏
分享

微信扫一扫

Linux下的awk用法详解

(Linux下的awk用法详解)

一、awk介绍

1.AWK 是一种处理文本文件的语言,是一个强大的文本分析工具。之所以叫 AWK 是因为其取了三位创始人 Alfred Aho,Peter Weinberger, 和 Brian Kernighan 的 Family Name 的首字符。 2.AWK拥有自己的语言: AWK 程序设计语言,它允许您创建简短的程序,这些程序读取输入文件、为数据排序、处理数据、对输入执行计算以及生成报表,还有无数其他的功能。

二、awk的语法

1.RHEL8.0系统上的awk帮助

[root@control scripts]# awk --help
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options:		GNU long options: (standard)
	-f progfile		--file=progfile
	-F fs			--field-separator=fs
	-v var=val		--assign=var=val
Short options:		GNU long options: (extensions)
	-b			--characters-as-bytes
	-c			--traditional
	-C			--copyright
	-d[file]		--dump-variables[=file]
	-D[file]		--debug[=file]
	-e 'program-text'	--source='program-text'
	-E file			--exec=file
	-g			--gen-pot
	-h			--help
	-i includefile		--include=includefile
	-l library		--load=library
	-L[fatal|invalid]	--lint[=fatal|invalid]
	-M			--bignum
	-N			--use-lc-numeric
	-n			--non-decimal-data
	-o[file]		--pretty-print[=file]
	-O			--optimize
	-p[file]		--profile[=file]
	-P			--posix
	-r			--re-interval
	-s			--no-optimize
	-S			--sandbox
	-t			--lint-old
	-V			--version

2.awk选项解释



-F fs or --field-separator fs
指定输入文件折分隔符,fs是一个字符串或者是一个正则表达式,如-F:。
-v var=value or --asign var=value
赋值一个用户定义变量。
-f scripfile or --file scriptfile
从脚本文件中读取awk命令。
-mf nnn and -mr nnn
对nnn值设置内在限制,-mf选项限制分配给nnn的最大块数目;-mr选项限制记录的最大数目。这两个功能是Bell实验室版awk的扩展功能,在标准awk中不适用。
-W compact or --compat, -W traditional or --traditional
在兼容模式下运行awk。所以gawk的行为和标准的awk完全一样,所有的awk扩展都被忽略。
-W copyleft or --copyleft, -W copyright or --copyright
打印简短的版权信息。
-W help or --help, -W usage or --usage
打印全部awk选项和每个选项的简短说明。
-W lint or --lint
打印不能向传统unix平台移植的结构的警告。
-W lint-old or --lint-old
打印关于不能向传统unix平台移植的结构的警告。
-W posix
打开兼容模式。但有以下限制,不识别:/x、函数关键字、func、换码序列以及当fs是一个空格时,将新行作为一个域分隔符;操作符**和**=不能代替^和^=;fflush无效。
-W re-interval or --re-inerval
允许间隔正则表达式的使用,参考(grep中的Posix字符类),如括号表达式[[:alpha:]]。
-W source program-text or --source program-text
使用program-text作为源代码,可与-f命令混用。
-W version or --version
打印bug报告信息的版本。

三、awk常见用法

1.文本字段处理

[root@control scripts]# awk '{print $0}' /etc/passwd |head
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

2.自定义字符段替换原文本的行输出

[root@control scripts]# awk '{print "this is a test line"}' /etc/fstab 
this is a test line
this is a test line
this is a test line
this is a test line
this is a test line
this is a test line
this is a test line
this is a test line
this is a test line
this is a test line
this is a test line
this is a test line
this is a test line
this is a test line
this is a test line
this is a test line

3.指定文本的列输出

[root@control scripts]# awk '{print $1}' /etc/fstab 

#
#
#
#
#
#
#
#
#
#
UUID=11568b58-6451-40da-a59f-7da0c2536cd3
UUID=59347c6a-20b5-4bc7-8b74-caa2980c6832
UUID=a94ebb71-f259-4589-9ad9-6ae3f495bc63
UUID=2f261207-03e5-47d7-ac83-9a4424fb6f74

4.指定文本的列输出,且列之间添加空格显示

[root@control scripts]# awk '{print $1 "\t " $3}' /etc/fstab 
UUID=11568b58-6451-40da-a59f-7da0c2536cd3	 xfs
UUID=59347c6a-20b5-4bc7-8b74-caa2980c6832	 xfs
UUID=a94ebb71-f259-4589-9ad9-6ae3f495bc63	 xfs
UUID=2f261207-03e5-47d7-ac83-9a4424fb6f74	 swap

四、awk其他用法

1.指定格式输出文本

[root@control scripts]# 
[root@control scripts]# awk -F":" '{print "USER: " $1  "\tSHELL "  $7}' /etc/passwd |head
USER: root	SHELL /bin/bash
USER: bin	SHELL /sbin/nologin
USER: daemon	SHELL /sbin/nologin
USER: adm	SHELL /sbin/nologin
USER: lp	SHELL /sbin/nologin
USER: sync	SHELL /bin/sync
USER: shutdown	SHELL /sbin/shutdown
USER: halt	SHELL /sbin/halt
USER: mail	SHELL /sbin/nologin
USER: operator	SHELL /sbin/nologin

2.设置多个字符为分隔符

[root@control scripts]# awk -F "[\t ]+" '{print "DEVICE: " $1 "\tFSTYPE: " $3}' /etc/fstab 
DEVICE: UUID=11568b58-6451-40da-a59f-7da0c2536cd3	FSTYPE: xfs
DEVICE: UUID=59347c6a-20b5-4bc7-8b74-caa2980c6832	FSTYPE: xfs
DEVICE: UUID=a94ebb71-f259-4589-9ad9-6ae3f495bc63	FSTYPE: xfs
DEVICE: UUID=2f261207-03e5-47d7-ac83-9a4424fb6f74	FSTYPE: swap
DEVICE: 	FSTYPE: 

五、awk语言特性

  • awk中的if语句
[root@control ~]# cat numtest.txt 
20 low
109 high
29 low
290 high
111 high

[root@control ~]# awk '{if ($1>100) print $1 " bad!"; else print "OK!"}' /root/numtest.txt 
OK!
109 bad!
OK!
290 bad!
111 bad!
OK!

备注:awk作为一门语言,支持语言的三种基本元素:顺序、判断、循环。所以awk可以支持if语句while/for循环,break和continue。

举报

相关推荐

0 条评论