码迷,mamicode.com
首页 > 其他好文 > 详细

awk命令

时间:2015-03-12 01:02:37      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:linux

awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大。简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理。


awk也有三种使用方式:
1.awk [选项] ‘awk命令’ 输入文件
2.awk [选项] -f awk脚本文件 输入文件
3. ./awk脚本文件 输入文件


1.输出文件中的空行


编写文件input01

1 hello world
2 
3 li hao 010232323
4 xiao ming 01034343
5 
6 wang bin 42323232
7 
8 



[root@iZ2546h6zurZ test]# awk '/^$/{print "this is a blank line"}' input01 
this is a blank line
this is a blank line
this is a blank line
this is a blank line


2.awk认为输入文件是结构化的,每行作为记录,行中的每个字符串定义为域,域之间用空格、tab、或其他字符隔开。


域:$1表示第一个域;$2表示第二个域;$0代表所有的域。
打印出input01文件指定的域
[root@iZ2546h6zurZ test]# awk '{print $2, $1, $3}' input01 
world hello 


hao li 010232323
ming xiao 01034343


bin wang 42323232




[root@iZ2546h6zurZ test]# awk '{print $0}' input01 
hello world


li hao 010232323
xiao ming 01034343


wang bin 42323232



3.在执行awk命令时定义变量


[root@iZ2546h6zurZ test]# awk 'BEGIN{one=1;two=2}{print $(one+two)}' input01 




010232323
01034343


42323232




4.关系运算符


匹配第一行的root,~匹配正则表达式,!~ 不匹配正则表达式
[root@iZ2546h6zurZ test]# awk 'BEGIN {FS=":"} $1~/root/' passwd 
root:x:0:0:root:/root:/bin/bash


其中FS=“:”是指定域之间的分隔符是:


打印出第三域小于第四域的记录

[root@iZ2546h6zurZ test]# awk 'BEGIN {FS=":"} {if ($3 < $4) print $0}' passwd 
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin



5.向awk脚本传递参数
编写pass.awk脚本
内容是:

#!/bin/awk -f
NF!=MAX
{print ("the line "NR" does not have "MAX" filds ")}


判断输入文件域是否等于最大值3,不等于则输出提示


[root@iZ2546h6zurZ test]# ./pass.awk MAX=3 FS=":" passwd
root:x:0:0:root:/root:/bin/bash
the line 1 does not have 3 filds 
bin:x:1:1:bin:/bin:/sbin/nologin
the line 2 does not have 3 filds 
daemon:x:2:2:daemon:/sbin:/sbin/nologin
the line 3 does not have 3 filds 
adm:x:3:4:adm:/var/adm:/sbin/nologin
the line 4 does not have 3 filds 
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
the line 5 does not have 3 filds 
sync:x:5:0:sync:/sbin:/bin/sync
the line 6 does not have 3 filds 
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
the line 7 does not have 3 filds 
halt:x:7:0:halt:/sbin:/sbin/halt
the line 8 does not have 3 filds 
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
the line 9 does not have 3 filds 
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
the line 10 does not have 3 filds 
operator:x:11:0:operator:/root:/sbin/nologin
the line 11 does not have 3 filds 
games:x:12:100:games:/usr/games:/sbin/nologin
the line 12 does not have 3 filds 
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
the line 13 does not have 3 filds 
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
the line 14 does not have 3 filds 
nobody:x:99:99:Nobody:/:/sbin/nologin
the line 15 does not have 3 filds 
dbus:x:81:81:System message bus:/:/sbin/nologin
the line 16 does not have 3 filds 
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
the line 17 does not have 3 filds 
abrt:x:173:173::/etc/abrt:/sbin/nologin
the line 18 does not have 3 filds 
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
the line 19 does not have 3 filds 
ntp:x:38:38::/etc/ntp:/sbin/nologin
the line 20 does not have 3 filds 
saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
the line 21 does not have 3 filds 
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
the line 22 does not have 3 filds 
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
the line 23 does not have 3 filds 
tcpdump:x:72:72::/:/sbin/nologin
the line 24 does not have 3 filds 
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
the line 25 does not have 3 filds 
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
the line 26 does not have 3 filds 
xiaojian:x:500:0::/usr/xiaojian:/bin/bash
the line 27 does not have 3 filds 



脚本中使用了awk的环境变量
NF表示当前记录中的域数量;
NR表示当前记录数;
NS表示字段分隔符;

awk命令

标签:linux

原文地址:http://blog.csdn.net/liuchangqing123/article/details/44208899

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!