AWK
[root@docker awk]# awk -F ':' '{print $1}' test.txt
root
bin
daemon
adm
$1 表示第一段,$0 表示全部 ,如果没有指定分隔符,使用空格作为分隔符。
[root@docker awk]# awk '{print $1}' 1.txt
a
ic
111
[root@docker awk]# awk -F ':' '{print $1,$3,$4}' test.txt
root 0 0
bin 1 1
daemon 2 2
adm 3 4
[root@docker awk]# awk -F ':' '{print $1"#"$3"#"$4}' test.txt 使用#来替换空格
root#0#0
bin#1#1
daemon#2#2
adm#3#4
lp#4#7
sync#5#0
shutdown#6#0
[root@docker awk]# awk '/oo/' test.txt --打印带oo的
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@docker awk]# awk -F ':' '$1~/oo/' test.txt --打印第一段带oo的行
root:x:0:0:root:/root:/bin/bash
[root@docker awk]# awk -F ':' '/root/ {print $1,$3} /user/ {print $1,$3,$4}' test.txt
root 0
operator 11
tss 59 59
rpcuser 29 29
[root@docker awk]# grep -E 'root|user' test.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
[root@docker awk]# awk -F ':' '/root|user/ {print $0}' test.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
[root@docker awk]# awk -F ':' '$3==0 {print $0}' test.txt
root:x:0:0:root:/root:/bin/bash
[root@docker awk]# awk -F ':' '$3>=1000 {print $0}' test.txt ---注意这里的1000如果是数字就不应该加""
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
awk -F ':' '$7!="/sbin/nologin"' /etc/passwd
awk -F ':' '$7!="/sbin/nologin"' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@docker awk]# awk -F ':' '$3<$4 {print$3,$4}' test.txt
3 4
4 7
8 12
12 100
14 50
[root@docker awk]# awk -F ':' '$3>"5" && $3<"7"' test.txt
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
[root@docker awk]# awk -F ':' '$3>1000 || $7=="/bin/bash"' /etc/passwd
root:x:0:0:root:/root:/bin/bash
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
head -5 /etc/passwd |awk -F ':' '{OFS="#"} {print $1,$3,$4}'
OFS 指定print的分隔符
[root@docker system]# head -5 /etc/passwd |awk -F ':' '{OFS="#"} {print $1,$3,$4}'
root#0#0
bin#1#1
daemon#2#2
adm#3#4
lp#4#7
可以带if
[root@docker system]# awk -F ':' '{OFS="#"} {if ($3>1000) {print $1,$2,$3,$4}}' /etc/passwd
nfsnobody#x#65534#65534
NR表示行
NF表示段
awk -F ':' '{print NR":" $0}' test.txt
awk -F ':' '{print NF":" $0}' test.txt
7:root:x:0:0:root:/root:/bin/bash
7:bin:x:1:1:bin:/bin:/sbin/nologin
7:daemon:x:2:2:daemon:/sbin:/sbin/nologin
7:adm:x:3:4:adm:/var/adm:/sbin/nologin
awk -F ':' 'NR<=10' test.txt
[root@docker awk]# awk -F ':' 'NR<=10' test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
awk -F ':' 'NR<=10 && $1 ~/root|sync/' test.txt ~表示包含
awk -F ':' 'NF==6 && $1 ~/root|sync/' test.txt
head -n 3 /etc/passwd |awk -F ':' '$1="root"'
head -n 3 test.txt |awk -F ':' '{OFS=":"} $1="root"'
awk -F ':' '{(tot=tot+$3)}; END {print tot}' /etc/passwd ---求和一列
awk -F ':' '{if ($1=="root") {print $0}}' /etc/passwd