首先创建一个文件叫做test
内容为(hehe前面有几个空格):
hehe lijie1:one lijie2:two lijie3:three
1.查找
awk '/lijie/' ./test
grep lijie ./test
2.默认查找分隔符
[root@lijie hadoop]# cat ./test | awk '{print $1}'
hehe
3.自定义分隔符
[root@lijie hadoop]# cat ./test | grep lijie | awk -F[:\ ] '{print $1}'
[root@lijie hadoop]# cat ./test | grep lijie | awk -F[:\ ] '{print $2}'
hehe
[root@lijie hadoop]# cat ./test | grep lijie | awk -F[:\ ] '{print $3}'
[root@lijie hadoop]# cat ./test | grep lijie | awk -F[:\ ] '{print $4}'
lijie1
[root@lijie hadoop]# cat ./test | grep lijie | awk -F[:\ ] '{print $5}'
4.
[root@lijie hadoop]# cat ./test | grep lijie | awk -F: '{print $1}'
hehe lijie1
5.空格
[root@lijie hadoop]# cat ./test | grep lijie | awk -F[\ ] '{print $2}'
hehe
6.自定义打印分隔符
[root@lijie hadoop]# cat ./test | grep lijie | awk -F: '{print $1"---"$2}'
hehe lijie1---one lijie2
7.$0 打印整行信息
[root@lijie hadoop]# cat ./test | grep lijie | awk -F: '{print $0}'
hehe lijie1:one lijie2:two lijie3:three
8.转义字符
[root@lijie hadoop]# cat ./test | awk -F[:] '{print $1"\t---\n"$2}'
hehe lijie1 ---
one lijie2
9.预定义表头
[root@lijie hadoop]# cat ./test | awk -F[:] 'BEGIN{print "table1 table2 table3"} {print $1,$2,$3}'
table1 table2 table3
hehe lijie1 one lijie2 two lijie3
10.预定义的分隔符
[root@lijie hadoop]# cat ./test | awk -F[:\ ] '{print $5}'
one
[root@lijie hadoop]# cat ./test | awk 'BEGIN{FS="[:\\ ]"}{print $5}'
one
11.预定义变量
[root@lijie hadoop]# cat ./test | awk 'BEGIN{FS="[:\\ ]";name="my name is LIJIE"}{print name,$5}'
my name is LIJIE one
12.awk的if else
[root@lijie hadoop]# cat ./test | awk 'BEGIN{FS=":"}{if ($1~"lijie12$")print $1;else print$2}'
one lijie2
[root@lijie hadoop]# cat ./test | awk 'BEGIN{FS=":"}{if ($1~"lijie1$")print $1;else print$2}'
hehe lijie1
--可以写成
[root@lijie hadoop]# cat ./test | awk -F: '{if($1~"lijie1$") print $1;else print$2}'
hehe lijie1
[root@lijie hadoop]# cat ./test | awk -F: '{if($1~"lijie12$") print $1;else print$2}'
one lijie2
--利用变量匹配
[root@lijie hadoop]# cat ./test | awk -F: 'BEGIN{name="lijie1$"} {if($1~name) print$1;elseprint$2}'
hehe lijie1
--逻辑判断
[root@lijie hadoop]# cat ./test | awk -F: 'BEGIN{name1="lijie1$";name2="lijie2$"} {if($1~name && $2~name2) print$1;elseprint$2}'
hehe lijie1
13.NF的用法
--取总共有多少列
[root@lijie hadoop]# cat ./test | awk -F: '{print NF}'
4
--取最后一个列
[root@lijie hadoop]# cat ./test | awk -F: '{print $NF}'
three
14.NR的用法(在之前test文件中添加一行heihei lijie666666:)
[root@lijie hadoop]# cat ./test | awk -F: '{print NR,$1}'
1 hehe lijie1
2 heihei lijie66666
15.自定义打印列分隔符(OFS)
[root@lijie hadoop]# cat ./test | awk -F: 'BEGIN{OFS="###"}{print $1,$2,$3}'
hehe lijie1###one lijie2###two lijie3
16.RS和ORS的使用(ORS行分隔符默认为 \n可以不写)
[root@lijie hadoop]# cat ./test | awk 'BEGIN{RS=":"}{print $1,$2,$3}'
hehe lijie1
one lijie2
two lijie3
three
17.awk的循环
[root@lijie hadoop]# cat ./test | awk 'BEGIN{FS=":"}{i=1; while(i<NF) {print $i" end";i++}}'
hehe lijie1 end
one lijie2 end
two lijie3 end
18.awk打印格式化(-表示左对齐,不加表示右对齐)
[root@lijie hadoop]# cat ./test | awk '{printf "%15-s %15s %-15s", $1,$2,$3}'
hehe lijie1:one lijie2:two
--如果printf后面有自定义的打印也需要格式化处理
[root@lijie hadoop]# cat ./test | awk '{printf "%15-s %15s %-15s %d", $1,$2,$3,1000}'
hehe lijie1:one lijie2:two 1000
注意:
NF 计算列数量
$NF 最后一列
NR 计算行号
数据截取定义符
RS 行分隔符
FS 列分隔符
数据打印定义符
OFS 列分隔符
ORS 行分割符