天天看點

awk

-F 指定分割符 ofs 是列印結果 print 指列印 

[root@abinlinux ~]# awk -F ':' '$1=="nobody" {print $1}' 1.txt

nobody

[root@abinlinux ~]# awk -F ':' '$1=="nobody"' 1.txt 比對一整行

nobody:x:99:99:Nobody:/:/sbin/nologin

[root@abinlinux ~]# awk -F ':' '$1=="nobady" || $7~/nolog/' 1.txt

作為連接配接符 或 并且作為連接配接符 

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

mail:x:8:12:mail:/var/spoorrrrrrrrol/mail:/sbin/nologin

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

[root@abinlinux ~]# awk -F ':' '$3>=500' 1.txt 判斷大于等于500

yun:x:500:500::/home/yun:/bin/bash

[root@abinlinux ~]# awk -F ':' '"$3>=500"' 1.txt 

加上雙引号 不再按照數字排序 按阿斯瑪排序 

rooooooooot:x:0:0:root:/root:/bin/bash

[root@abinlinux ~]# awk -F ':' '$3>=500' 1.txt 要跟數字比的話要把雙引号去掉 

[root@abinlinux ~]# awk -F ':' '$7!~/nolog/' 1.txt 不比對nolog 

sync:x:5:0:sync:/rrr_orbin:/bin/sync

[root@abinlinux ~]# awk -F ':' '$3<$4' 1.txt $3<$4的行 

[root@abinlinux ~]# awk -F ':' '$3==$4' 1.txt $3=$4 的比對 不能寫一個等于号 一個是指派

[root@abinlinux ~]# awk -F ':' 'OFS=":" ;$3=$4' 1.txt 

[root@abinlinux ~]# awk -F ':' 'OFS=":" ;$3=$4' 1.txt $3=$4 3的值比對給4 ofs以冒号分開

[root@abinlinux ~]# awk -F ':' 'NR<10' 1.txt 小于10的行顯示出來 

NR 行 

[root@abinlinux ~]# awk -F ':' 'NR>10' 1.txt 大于10 的行顯示出來 

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/goproher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

[root@abinlinux ~]# awk -F ':' 'NR==10' 1.txt 等于 10 的行 

[root@abinlinux ~]# awk -F ':' 'NR==10 {print $1,$7}' 1.txt 隻列印指定的段 

uucp /sbin/nologin

[root@abinlinux ~]# awk -F ':' '{if(NR==10) print $1,$7}' 1.txt if的用法 

[root@abinlinux ~]# awk -F ':' 'OFS=":" {if(NR==10) print $1,$7}' 1.txt 以:分割符用法 

uucp:/sbin/nologin

[root@abinlinux ~]# awk -F ':' '{print NF}' 1.txt 直接列印沒一行有幾段 

7

[root@abinlinux ~]# awk -F ':' '{if (NF==7) print $1}' 1.txt 判斷每行是不是七段 并列印$1

rooooooooot

bin

daemon

adm

[root@abinlinux ~]# awk -F ':' 'OFS=":" ;$7=$3+$4' 1.txt 

bin:x:1:1:bin:/bin:2

daemon:x:2:2:daemon:/sbin:4

[root@abinlinux ~]# awk -F ':' 'OFS=":" {$7=$3+$4; print $0}' 1.txt $0是正行 

rooooooooot:x:0:0:root:/root:0

adm:x:3:4:adm:/var/adm:7

lp:x:4:7:lp:/var/spool/lpd:11

[root@abinlinux ~]# awk -F ':' 'OFS=":" {$7=$3+$4; print $1,$3}' 1.txt

rooooooooot:0

bin:1

daemon:2

adm:3

本文轉自 amenging 51CTO部落格,原文連結:http://blog.51cto.com/11335852/2043426