Shell命令_awk命令
# awk ‘条件1{动作1} 条件2{动作2}…’ 文件名
条件( Pattern):
一般使用关系表达式作为条件
x > 10 判断变量 x是否大于10
x>=10 大于等于
x<=10 小于等于
动作( Action):
格式化输出
流程控制语句
文本信息:vim score.txt
1 2 3 4 | ID Name PHP Linux MySQL Average 1 Liming 82 95 86 87.66 2 Sc 74 96 87 85.66 3 Gao 99 83 93 91.66 |
1、 输出score.txt每行内容,第2行+\t+第6行+\n
printf行尾不输出换行;print行为输出换行
1 2 3 4 5 6 7 8 9 10 | [root@V2 tmp] # cat score.txt D Name PHP Linux MySQL Average 1 Liming 82 95 86 87.66 2 Sc 74 96 87 85.66 3 Gao 99 83 93 91.66 [root@V2 tmp] # awk '{printf $2 "\t" $6 "\n"}' score.txt Name Average Liming 87.66 Sc 85.66 Gao 91.66 |
2、输出df第1,3行内容
1 2 3 4 5 6 7 8 9 10 | [root@V2 tmp] # df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_v2-lv_root 36G 5.0G 29G 15% / tmpfs 931M 72K 931M 1% /dev/shm /dev/sda1 485M 40M 421M 9% /boot [root@V2 tmp] # df -h | awk '{print $1 "\t" $3}' Filesystem Used /dev/mapper/vg_v2-lv_root 5.0G tmpfs 72K /dev/sda1 40M |
3、输出下面内人中的1
1 | df -h| grep tmpfs| awk '{print $5}' | cut -d "%" -f 1 |
4、BEGIN
1 2 3 4 5 6 | [root@V2 tmp] # awk 'BEGIN{print "这里是开始前输出"}{printf $2 "\t" $6 "\n"}' score.txt 这里是开始前输出 Name Average Liming 87.66 Sc 85.66 Gao 91.66 |
5、END
1 2 3 4 5 6 7 | [root@V2 tmp] # awk 'BEGIN{print "这里是开始前输出"}END{print "这里是结束输出"}{printf $2 "\t" $6 "\n"}' score.txt 这里是开始前输出 Name Average Liming 87.66 Sc 85.66 Gao 91.66 这里是结束输出 |
6、FS设置分隔符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | [root@V2 tmp] # cat /etc/passwd | grep "/bin/bash" | awk 'BEGIN {FS=":"} {printf $1 "\t" $3 "\n"}' root 0 chenzhi 500 mysql 27 [root@V2 tmp] # cat /etc/passwd root:x:0:0:root: /root : /bin/bash bin:x:1:1:bin: /bin : /sbin/nologin daemon:x:2:2:daemon: /sbin : /sbin/nologin adm:x:3:4:adm: /var/adm : /sbin/nologin lp:x:4:7:lp: /var/spool/lpd : /sbin/nologin sync :x:5:0: sync : /sbin : /bin/sync shutdown :x:6:0: shutdown : /sbin : /sbin/shutdown halt:x:7:0:halt: /sbin : /sbin/halt mail:x:8:12:mail: /var/spool/mail : /sbin/nologin uucp:x:10:14:uucp: /var/spool/uucp : /sbin/nologin operator:x:11:0:operator: /root : /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 nobody:x:99:99:Nobody:/: /sbin/nologin dbus:x:81:81:System message bus:/: /sbin/nologin usbmuxd:x:113:113:usbmuxd user:/: /sbin/nologin vcsa:x:69:69:virtual console memory owner: /dev : /sbin/nologin rpc:x:32:32:Rpcbind Daemon: /var/cache/rpcbind : /sbin/nologin rtkit:x:499:497:RealtimeKit: /proc : /sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack: /var/lib/avahi-autoipd : /sbin/nologin abrt:x:173:173:: /etc/abrt : /sbin/nologin rpcuser:x:29:29:RPC Service User: /var/lib/nfs : /sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User: /var/lib/nfs : /sbin/nologin haldaemon:x:68:68:HAL daemon:/: /sbin/nologin gdm:x:42:42:: /var/lib/gdm : /sbin/nologin ntp:x:38:38:: /etc/ntp : /sbin/nologin apache:x:48:48:Apache: /var/www : /sbin/nologin saslauth:x:498:76: "Saslauthd user" : /var/empty/saslauth : /sbin/nologin postfix:x:89:89:: /var/spool/postfix : /sbin/nologin pulse:x:497:496:PulseAudio System Daemon: /var/run/pulse : /sbin/nologin sshd:x:74:74:Privilege-separated SSH: /var/empty/sshd : /sbin/nologin tcpdump:x:72:72::/: /sbin/nologin chenzhi:x:500:500:chenzhi: /home/chenzhi : /bin/bash
[root@V2 tmp] # cat /etc/passwd | grep "/bin/bash" | awk 'BEGIN {FS=":"} {printf $1 "\t" $3 "\n"}' root 0 chenzhi 500 mysql 27 |
7、逻辑运算
grep -v Name 删除包含内容为"Name"的行
awk '$6 >= 87 {printf $2 "\n" } 计算第6行大于87的
1 2 3 4 5 6 7 8 | [root@V2 tmp] # cat score.txt D Name PHP Linux MySQL Average 1 Liming 82 95 86 87.66 2 Sc 74 96 87 85.66 3 Gao 99 83 93 91.66 [root@V2 tmp] # cat score.txt | grep -v Name | awk '$6 >= 87 {printf $2 "\n" }' Liming Gao |