15
2018
07

awk常用方法

1、
准备测试文件
[root@VM_0_17_centos curl_demo]# more log.txt
2 this is a test
3 Are you like awk
This's a test
10 There are orange,apple,mongo


2、

格式化输出
[root@VM_0_17_centos curl_demo]# awk '{print $1,$4}' log.txt
2 a
3 like
This's
10 orange,apple,mongo


3、

错误实例
[root@VM_0_17_centos curl_demo]# awk log.txt '{print $1,$4}'
awk: cmd. line:1: log.txt
awk: cmd. line:1:    ^ syntax error


4、

以逗号或者空格作为分隔符
[root@VM_0_17_centos curl_demo]# ll
total 4
-rw-r--r-- 1 root root 82 Jul 15 18:06 log.txt
[root@VM_0_17_centos curl_demo]#  awk -F, '{print $1,$2}'   log.txt
2 this is a test
3 Are you like awk
This's a test
10 There are orange apple


5、条件输出

[root@VM_0_17_centos curl_demo]# awk '$1>2 && $2=="Are" {print $1,$2,$3}' log.txt
3 Are you


6、

复杂条件,复杂格式输出
[root@VM_0_17_centos curl_demo]# awk 'BEGIN{printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n","FILENAME","ARGC","FNR","FS","NF","NR","OFS","ORS","RS";printf "---------------------------------------------\n"} {printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s",FILENAME,ARGC,FNR,FS,NF,NR,OFS,ORS,RS}'  log.txt
FILENAME ARGC  FNR   FS   NF   NR  OFS  ORS   RS
---------------------------------------------
log.txt    2    1         5    1
log.txt    2    2         5    2
log.txt    2    3         3    3
log.txt    2    4         4    4


refer:

包含命令的具体信息


« 上一篇

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Top