|
Linux awk命令使用
目录
笔记日期20180408
awk
1. print
2. 变量FS,OFS,NF,NR
3. printf命令
4. 操作符
5. PATTERN
Introduction
Gawk is the GNU Project's implementation of the AWK programming lan-
guage. It conforms to the definition of the language in the POSIX
1003.1 Standard. This version in turn is based on the description in
The AWK Programming Language, by Aho, Kernighan, and Weinberger. Gawk
provides the additional features found in the current version of UNIX
awk and a number of GNU-specific extensions.
The command line consists of options to gawk itself, the AWK program
text (if not supplied via the -f or --file options), and values to be
made available in the ARGC and ARGV pre-defined AWK variables.
Pgawk is the profiling version of gawk. It is identical in every way
to gawk, except that programs run more slowly, and it automatically
produces an execution profile in the file awkprof.out when done. See
the --profile option, below.
Dgawk is an awk debugger. Instead of running the program directly, it
loads the AWK source code and then prompts for debugging commands.
Unlike gawk and pgawk, dgawk only processes AWK program source pro?
vided with the -f option. The debugger is documented in GAWK: Effec?
tive AWK Programming.
awk 报告生成工具,格式化文本输出
gawk - pattern scanning and processing language
基本用法: gawk [options] 'program' file ...
program: PATTERN{ACTION STATEMENTS}
语句之间分号分隔
选项:
-F 指明输入时用到的字段分隔符
-v var=value: 自定义变量
1. print
print item1, item2, ...
要点:
(1) 逗号分隔符
(2) 输出的各item可以字符串,也可以是数值,当前记录的字段、变量、或awk表达式
(3) 如省略item,相当于print $0 即全部输出
# awk -F: '{print "hello " $1,$2}' /etc/passwd
每行会打印一个hello字符
2. 变量
2.1 内建变量
此处只给命令行,执行效果,有兴趣可以直接放到命令行里查看
FS: input field seperator, 默认为空白字符
OFS: output field seperator, 默认为空白字符
# awk -v FS=":" -v OFS=" ++ " '{print $1,$2}' /etc/passwd
# awk '{FS=":";OSF=" ++ ";print $1,$2}' /etc/passwd
两条命令含义一样,只是不同的格式
RS: input record seperator,输入时的换行符
ORS: output record seperator, 输出时的换行符
# awk '{RS="\n";ORS="--------";print $0}' /etc/passwd
这个表示更换换行符
NF: number of field, 每一行的字段数
# awk '{print NF}' /etc/fstab
打印出每一行的字段数
# awk '{print $NF}' /etc/fstab
打印出最后一个字段的字符串,是因为将变量替换成数值,而数字加$符号表示打印
具体某个字段,所以最后才会出现打印最后一个字段的字符串
NR: number of record, 显示文件的行数
# awk '{print NR}' /etc/fstab
FILENAME: 当前文件名
# awk '{print FILENAME}' /etc/fstab
ARGC: 命令行参数个数
# awk 'BEGIN{print ARGC}' /etc/fstab
这个会打印出2含义是awk这个命令后跟了两个参数
ARGV: 数组,保存的是命令行所给定的各参数
# awk '{print ARGV[1]}' /etc/fstab /etc/passwd
2.2 自定义变量
(1) -v var=value
变量区分字符大小写(在此前已经用过,只不过那是内建,此处只需要自定义即可)
# awk -v HE="hello" '{print HE,$1}' /etc/fstab
(2)在program中直接定义
# awk '{HE="hello";print HE,$1}' /etc/fstab
含义与上方的命令一样
3. printf命令
格式化输入: printf FORMAT, item1, item2, ...
(1) FORMAT必须要给出
(2) 不会自动换行,需要显式给出换行控制符,\n
(3) FORMAT中需要分别为后面的每个item指定一个格式化符号;
格式符(风格是C语言的,如果学过C应该很容易理解)
%c 显示字符的ASCII码;
%d,%i 显示十进制整数
%e,%E 科学计数法数值显示
%f 显示为浮点数
%g,%G 以科学计数法或浮点形式显示数值
%s 显示字符串
%u 无符号整数
%% 显示%自身
修饰符(也是C语言里的东东)
用来格式化输出,如每个字段给几个字符等
#[.#] 第一个数字控制显示的宽度,第二个#表示小数点后的精度
%3.1f
- 左对齐
+ 显示数值的符号
# awk -F: '{printf "UserName: %s\n",$1}' /etc/passwd
将所有的用户名打印出来,并在其前方加入Username:
# awk -F: '{printf "UserName: %-30s UserID: %s\n",$1,$3}' /etc/passwd
这条效果更工整
4. 操作符
算术操作符
x+y, x-y, x*y, x/y, x^y, x%y
-x
+x 转换为数值
字符串操作符:没有符号的操作符,字符串连接
赋值操作符
=, +=, -=, *=, /=, %=, ^=
++, --
比较操作符
>, >=, |
|