shell training day5 8.19

AWK

[root@docker awk]# awk -F ':' '{print $1}' test.txt
root
bin
daemon
adm

$ 1 represents the first stage, all $ 0 indicates, if not specified delimiter and a space as a separator.

[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 replaced spaces using #
the root # 0 # 0
bin #. 1 #. 1
daemon # 2 # 2
# #. 4. 3 ADM
LP #. 4 #. 7
Sync # # 0. 5
the 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 line with the first section print
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 Note here should not be added if the number is ""
nfsnobody is: 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

-5 head / etc / the passwd | awk -F ':' 'the OFS = { "#"} {$ print. 1,. 3 $, $. 4}'
the OFS specified print delimiter

[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

Can take IF
[the root @ Docker System] # awk -F ':' '{the OFS = "#"} {IF ($. 3> 1000) {Print $. 1, $ 2, $. 3, $. 4}}' / etc / the passwd
nfsnobody is # X # 65534 # 65534

NR indicates a row

NF represents a segment

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 ~ denotes comprising

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 --- a summation

awk -F ':' '{if ($1=="root") {print $0}}' /etc/passwd

Guess you like

Origin blog.51cto.com/testdb/2431364