标签:linux
awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大。简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理。编写文件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
[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
[root@iZ2546h6zurZ test]# awk 'BEGIN{one=1;two=2}{print $(one+two)}' input01 010232323 01034343 42323232
[root@iZ2546h6zurZ test]# awk 'BEGIN {FS=":"} $1~/root/' passwd root:x:0:0:root:/root:/bin/bash
[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
#!/bin/awk -f NF!=MAX {print ("the line "NR" does not have "MAX" filds ")}
[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
标签:linux
原文地址:http://blog.csdn.net/liuchangqing123/article/details/44208899