Catalog
1 awk command
- Copy exercise files
[root@worker1 dir9]# cp /etc/passwd ./testpasswd
- Print the first field
[root@worker1 dir9]# head -n2 testpasswd | awk -F ':' '{print $1}' ROOT bin
- Print entire line
[root@worker1 dir9]# head -n2 testpasswd | awk -F ':' '{print $0}' ROOT:x:0:0:roooot:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin
- Print the first field, the second field, the third field, the fourth field, the print separator is ""
[root@worker1 dir9]# awk -F ':' '{print $1"#"$2"#"$3"#"$4}' testpasswd ROOT#x#0#0 bin#x#1#1 daemon#x#2#2 adm#x#3#4 lp#x#4#7 sync#x#5#0 shutdown#x#6#0 halt#x#7#0 mail#x#8#12 operator#x#11#0 games#x#12#100 ftp#x#14#50 nobody#x#99#99 avahi-autoipd#x#170#170 systemd-bus-proxy#x#999#997 systemd-network#x#998#996 dbus#x#81#81 polkitd#x#997#995 tss#x#59#59 postfix#x#89#89 sshd#x#74#74 user#x#1000#1000 user1#x#1001#1001 user2#x#1002#1100 user3#x#1111#1100 user5#x#1112#1112 user6#x#1113#1113 ntp#x#38#38 tcpdump#x#72#72 BUS123###
- Matching character "oo"
[root@worker1 dir9]# awk '/oo/' testpasswd ROOT:x:0:0:roooot:/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
- The first field matches the character "adm"
[root@worker1 dir9]# awk -F ':' '$1 ~/adm/' testpasswd adm:x:3:4:adm:/var/adm:/sbin/nologin
- Print the first field and the third field of the matching character "root"; print the first field, the third field and the fourth field of the matching character "adm"
[root@worker1 dir9]# awk -F ':' '/root/ {print $1,$3} /adm/ {print $1,$3,$4}' testpasswd ROOT 0 adm 3 4 operator 11
- The third field is a line with a character value of 0
[root@worker1 dir9]# awk -F ':' '$3=="0"' testpasswd ROOT:x:0:0:roooot:/root:/bin/bash
- Lines whose third field is greater than or equal to a character value of 500 (in ASCII code form)
[root@worker1 dir9]# awk -F ':' '$3>="500"' testpasswd 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 systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:997:995: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 tcpdump:x:72:72::/:/sbin/nologin
- Lines with a value greater than or equal to 500 in the third field
[root@worker1 dir9]# awk -F ':' '$3>=500' testpasswd systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin polkitd:x:997:995:User for polkitd:/:/sbin/nologin user:x:1000:1000::/home/user:/bin/bash user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1100::/home/user2:/bin/bash user3:x:1111:1100::/home/user3:/bin/bash user5:x:1112:1112::/home/user5:/bin/bash user6:x:1113:1113::/home/user6:/bin/bash
- Lines with the seventh field value character "/sbin/nologin"
[root@worker1 dir9]# awk -F ":" '$7!="/sbin/nologin"' testpasswd ROOT:x:0:0:roooot:/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 user:x:1000:1000::/home/user:/bin/bash user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1100::/home/user2:/bin/bash user3:x:1111:1100::/home/user3:/bin/bash user5:x:1112:1112::/home/user5:/bin/bash user6:x:1113:1113::/home/user6:/bin/bash BUS123
- Print rows with a fourth field larger than a third field
[root@worker1 dir9]# awk -F ":" '$3>$4' testpasswd 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 systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin polkitd:x:997:995:User for polkitd:/:/sbin/nologin user3:x:1111:1100::/home/user3:/bin/bash
- Print lines with a third field larger than the fifth field and a third field larger than a character value of 7
[root@worker1 dir9]# awk -F ":" '$3>"5" && $3<"7"' testpasswd 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
- Print a numeric value greater than 1000 in the third field or a row with the seventh field equal to the character "/bin/bash"
[root@worker1 dir9]# awk -F ":" '$3>1000||$7=="/bin/bash"' testpasswd ROOT:x:0:0:roooot:/root:/bin/bash user:x:1000:1000::/home/user:/bin/bash user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1100::/home/user2:/bin/bash user3:x:1111:1100::/home/user3:/bin/bash user5:x:1112:1112::/home/user5:/bin/bash user6:x:1113:1113::/home/user6:/bin/bash
- Print fields 1,3,4 with the spacer ""
[root@worker1 dir9]# head -5 testpasswd | awk -F ":" '{OFS="#"} {print $1,$3,$4}' ROOT#0#0 bin#1#1 daemon#2#2 adm#3#4 lp#4#7
- Conditional Judgment: The third field is greater than 1000 values, print the first, third, and fourth fields, and the spacer is ""
[root@worker1 dir9]# awk -F ":" '{OFS="#"} { if($3>1000) {print $1,$3,$4}}' testpasswd user1#1001#1001 user2#1002#1100 user3#1111#1100 user5#1112#1112 user6#1113#1113
- Print the number of fields separated by ":"
[root@worker1 dir9]# head -n5 testpasswd | awk -F ":" '{print NF}' 7 7 7 7 7
- Print line numbers separated by ":"
[root@worker1 dir9]# head -n5 testpasswd | awk -F ":" '{print NR}' 1 2 3 4 5
- Print lines with line numbers greater than 20
[root@worker1 dir9]# awk 'NR>20' testpasswd sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin user:x:1000:1000::/home/user:/bin/bash user1:x:1001:1001::/home/user1:/bin/bash user2:x:1002:1100::/home/user2:/bin/bash user3:x:1111:1100::/home/user3:/bin/bash user5:x:1112:1112::/home/user5:/bin/bash user6:x:1113:1113::/home/user6:/bin/bash ntp:x:38:38::/etc/ntp:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin BUS123
- Print lines with a line number less than 20 and match the character "roo"
[root@worker1 dir9]# awk 'NR<20 && $1 ~ /roo/' testpasswd ROOT:x:0:0:roooot:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
- Print the first field equal to the character "bin"
[root@worker1 dir9]# head -n3 testpasswd | awk -F ":" '$1=="bin"' bin:x:1:1:bin:/bin:/sbin/nologin
- Calculate the sum of the third field
[root@worker1 dir9]# awk -F ':' '{sum=sum+$3} END {print sum}' testpasswd 10088
Conditional Matching: Print the line with the first field equal to the character "bin"
[root@worker1 dir9]# awk -F ':' '$1=="bin" {print $0}' testpasswd bin:x:1:1:bin:/bin:/sbin/nologin
extend
Practice all exercises. http://www.apelearn.com/study_v2/chapter14.html