[root@linux-128 ~]# awk '/root/' 1.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
2 awk能夠作到匹配某一段中的某個字符,以:分隔,匹配第一段中帶有root的行linux
[root@linux-128 ~]# awk -F ':' '$1 ~ /root/' 1.txt root:x:0:0:root:/root:/bin/bash
[root@linux-128 ~]# awk '/ooo?/' 1.txt root:x:0:0:root:/root:/bin/bash lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[root@linux-128 ~]# awk -F ':' '/oo+/' 1.txt root:x:0:0:root:/root:/bin/bash lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin
grep -Ebash
[root@linux-128 ~]# grep -E 'oo+' 1.txt root:x:0:0:root:/root:/bin/bash lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sed –rssh
[root@linux-128 ~]# sed -n -r '/oo+/'p 1.txt root:x:0:0:root:/root:/bin/bash lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[root@linux-128 ~]# head -5 1.txt |awk -F ':' '{print $1}' root bin daemon adm lp
[root@linux-128 ~]# head -5 1.txt |awk -F ':' '{print $0}' 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
[root@linux-128 ~]# head -5 1.txt |awk -F ':' '{print $1,$2,$3,$4}' root x 0 0 bin x 1 1 daemon x 2 2 adm x 3 4 lp x 4 7
[root@linux-128 ~]# head -5 1.txt |awk -F ':' '{print $1"#"$2"#"$3"#"$4}' root#x#0#0 bin#x#1#1 daemon#x#2#2 adm#x#3#4 lp#x#4#7
[root@linux-128 ~]# awk -F ':' '/root/{print $1,$3} /user/ {print $1,$3,$4}' 1.txt root 0 operator 11 tss 59 59 user3 1004 1003 user4 1005 1003 user5 1007 1006 user6 1008 1003 user7 1009 1009
[root@linux-128 ~]# awk '/root|bash/' 1.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin wuzhou:x:1000:1000::/home/wuzhou:/bin/bash user7:x:1009:1009::/home/user7:/bin/bash
[root@linux-128 ~]# awk '/root/||/bash/' 1.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin wuzhou:x:1000:1000::/home/wuzhou:/bin/bash user7:x:1009:1009::/home/user7:/bin/bash
大於編輯器
= 大於等於post
[root@linux-128 ~]# awk -F ':' '$3=='0'' 1.txt
[root@linux-128 ~]# awk -F ':' '$3>='500'' 1.txt systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin polkitd:x:998:996:User for polkitd:/:/sbin/nologin chrony:x:997:995::/var/lib/chrony:/sbin/nologin wuzhou:x:1000:1000::/home/wuzhou:/bin/bash user3:x:1004:1003::/home/user3:/sbin/nolongin user4:x:1005:1003::/home/user4:/sbin/nolongin user5:x:1007:1006::/home/user5:/bin/login user6:x:1008:1003::/home/wuzhou:/sbin/nologin user7:x:1009:1009::/home/user7:/bin/bash
[root@linux-128 ~]# awk -F ':' '$3>="500"' 1.txt 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 nobody:x:99:99:Nobody:/:/sbin/nologin systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:998:996:User for polkitd:/:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin chrony:x:997:995::/var/lib/chrony:/sbin/nologin
[root@linux-128 ~]# awk -F ':' '$7!="/sbin/nologin"' 1.txt root:x:0:0:root:/root:/bin/bash sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt wuzhou:x:1000:1000::/home/wuzhou:/bin/bash user3:x:1004:1003::/home/user3:/sbin/nolongin user4:x:1005:1003::/home/user4:/sbin/nolongin user5:x:1007:1006::/home/user5:/bin/login user7:x:1009:1009::/home/user7:/bin/bash
[root@linux-128 ~]# awk -F ':' '$3>'1000' && $3<'1009'' 1.txt user3:x:1004:1003::/home/user3:/sbin/nolongin user4:x:1005:1003::/home/user4:/sbin/nolongin user5:x:1007:1006::/home/user5:/bin/login user6:x:1008:1003::/home/wuzhou:/sbin/nologin
[root@linux-128 ~]# awk -F ':' '$3>'1000' || $7=="/bin/bash"' 1.txt root:x:0:0:root:/root:/bin/bash wuzhou:x:1000:1000::/home/wuzhou:/bin/bash user3:x:1004:1003::/home/user3:/sbin/nolongin user4:x:1005:1003::/home/user4:/sbin/nolongin user5:x:1007:1006::/home/user5:/bin/login user6:x:1008:1003::/home/wuzhou:/sbin/nologin user7:x:1009:1009::/home/user7:/bin/bash
[root@linux-128 ~]# awk -F ':' '$3>$4' 1.txt sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt operator:x:11:0:operator:/root:/sbin/nologin systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin polkitd:x:998:996:User for polkitd:/:/sbin/nologin chrony:x:997:995::/var/lib/chrony:/sbin/nologin user3:x:1004:1003::/home/user3:/sbin/nolongin user4:x:1005:1003::/home/user4:/sbin/nolongin user5:x:1007:1006::/home/user5:/bin/login user6:x:1008:1003::/home/wuzhou:/sbin/nologin
[root@linux-128 ~]# awk -F ':' '{OFS="#"} $3>"5" {print $1,$2,$3,$4}' 1.txt shutdown#x#6#0 halt#x#7#0 mail#x#8#12 nobody#x#99#99 systemd-bus-proxy#x#999#997 dbus#x#81#81 polkitd#x#998#996 tss#x#59#59 postfix#x#89#89 sshd#x#74#74 chrony#x#997#995
[root@linux-128 ~]# head -5 1.txt|awk -F ':' '{print NR":"$0}' 1:root:x:0:0:root:/root:/bin/bash 2:bin:x:1:1:bin:/bin:/sbin/nologin 3:daemon:x:2:2:daemon:/sbin:/sbin/nologin 4:adm:x:3:4:adm:/var/adm:/sbin/nologin 5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@linux-128 ~]# awk -F ':' 'NR>20 {print NR":"$0}' 1.txt 21:chrony:x:997:995::/var/lib/chrony:/sbin/nologin 22:wuzhou:x:1000:1000::/home/wuzhou:/bin/bash 23:user3:x:1004:1003::/home/user3:/sbin/nolongin 24:user4:x:1005:1003::/home/user4:/sbin/nolongin 25:user5:x:1007:1006::/home/user5:/bin/login 26:user6:x:1008:1003::/home/wuzhou:/sbin/nologin 27:user7:x:1009:1009::/home/user7:/bin/bash
[root@linux-128 ~]# awk -F ':' '{OFS="#"} {if($3>'1000'){print $1,$2,$3}}' 1.txt user3#x#1004 user4#x#1005 user5#x#1007 user6#x#1008 user7#x#1009
[root@linux-128 ~]# head -5 1.txt|awk -F ':' '$1="root"' root x 0 0 root /root /bin/bash root x 1 1 bin /bin /sbin/nologin root x 2 2 daemon /sbin /sbin/nologin root x 3 4 adm /var/adm /sbin/nologin root x 4 7 lp /var/spool/lpd /sbin/nologin
[root@linux-128 ~]# awk -F ':' '{(oto=oto+$3)}; END {print oto}' 1.txt 9694
[root@linux-128 ~]# awk -F ':' '{if ($1=="root") {print $0}}' 1.txt root:x:0:0:root:/root:/bin/bash