9.6/9.7 awk

 

[root@lizhipeng01 ~]# mkdir awk
[root@lizhipeng01 ~]# cp /etc/passwd awk/test.txt
[root@lizhipeng01 ~]# cd awk
[root@lizhipeng01 awk]# ls
test.txt
[root@lizhipeng01 awk]# awk -F ':' '{print $1}' test
awk: fatal: cannot open file `test' for reading (没有那个文件或目录)
[root@lizhipeng01 awk]# awk -F ':' '{print $1}' test.txt                      打印第一段
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
avahi-autoipd
dbus
polkitd
tss
postfix
sshd
lizhipeng
user1
lzp
user2
user3
user4
user5

[root@lizhipeng01 awk]# awk -F ':' '{print $0}' test.txt              $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
sync:x:5:0:sync:/sbin:/bin/sync
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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998: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
lizhipeng:x:1000:1000::/home/lizhipeng:/bin/bash
user1:x:1001:1001::/home/user1:/bin/bash
lzp:x:1002:1003::/home/lzp:/bin/bash
user2:x:1003:1006::/home/user2:/bin/bash
user3:x:1004:1005::/home/user3:/bin/bash
user4:x:1006:1005::/home/lizhipeng001:/sbin/nologin
user5:x:1007:1007::/home/user5:/bin/bash

[root@lizhipeng01 awk]# awk -F ':' '{print $1"#"$2"#"$3}' test.txt
root#x#0
bin#x#1
daemon#x#2
adm#x#3
lp#x#4
sync#x#5
shutdown#x#6
halt#x#7
mail#x#8
operator#x#11
games#x#12
ftp#x#14
nobody#x#99
avahi-autoipd#x#170
dbus#x#81
polkitd#x#999
tss#x#59
postfix#x#89
sshd#x#74
lizhipeng#x#1000
user1#x#1001
lzp#x#1002
user2#x#1003
user3#x#1004
user4#x#1006
user5#x#1007

[root@lizhipeng01 awk]# awk -F ':' '$1 ~ /oo/' test.txt      第一段匹配oo的行
root:x:0:0:root:/root:/bin/bash

[root@lizhipeng01 awk]# awk -F ':' '$1 ~ /o+/' test.txt
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
operator:x:11:0:operator:/root:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin

[root@lizhipeng01 awk]# awk -F ':' '/root/ {print $1,$3} /user/ {print $3,$4}' test.txt
root 0
operator 11
59 59
1001 1001
1003 1006
1004 1005
1006 1005
1007 1007
[root@lizhipeng01 awk]# awk -F ':' '/root/ {print $1,$3} /user/ {print $1,$3,$4}' test.txt
root 0
operator 11
tss 59 59
user1 1001 1001
user2 1003 1006
user3 1004 1005
user4 1006 1005
user5 1007 1007
[root@lizhipeng01 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
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1003:1006::/home/user2:/bin/bash
user3:x:1004:1005::/home/user3:/bin/bash
user4:x:1006:1005::/home/lizhipeng001:/sbin/nologin
user5:x:1007:1007::/home/user5:/bin/bash

[root@lizhipeng01 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
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1003:1006::/home/user2:/bin/bash
user3:x:1004:1005::/home/user3:/bin/bash
user4:x:1006:1005::/home/lizhipeng001:/sbin/nologin
user5:x:1007:1007::/home/user5:/bin/bash

[root@lizhipeng01 awk]# awk -F ':' '$3==0' test.txt
root:x:0:0:root:/root:/bin/bash

[root@lizhipeng01 awk]# awk -F ':' '$3==0' test.txt
root:x:0:0:root:/root:/bin/bash
[root@lizhipeng01 awk]# awk -F ':' '$3==0 {print $1}' test.txt
root
[root@lizhipeng01 awk]# awk -F ':' '$3>=1000 {print $1}' test.txt
lizhipeng
user1
lzp
user2
user3
user4
user5

[root@lizhipeng01 awk]# awk -F ':' '$7!="/bin/nologin" {print $0}' test.txt
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
sync:x:5:0:sync:/sbin:/bin/sync
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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998: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
lizhipeng:x:1000:1000::/home/lizhipeng:/bin/bash
user1:x:1001:1001::/home/user1:/bin/bash
lzp:x:1002:1003::/home/lzp:/bin/bash
user2:x:1003:1006::/home/user2:/bin/bash
user3:x:1004:1005::/home/user3:/bin/bash
user4:x:1006:1005::/home/lizhipeng001:/sbin/nologin
user5:x:1007:1007::/home/user5:/bin/bash

[root@lizhipeng01 awk]# awk -F ':' '$3<$4' test.txt
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/spool/mail:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
lzp:x:1002:1003::/home/lzp:/bin/bash
user2:x:1003:1006::/home/user2:/bin/bash
user3:x:1004:1005::/home/user3:/bin/bash

[root@lizhipeng01 awk]# awk -F ':' '$3==$4' test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
dbus:x:81:81:System message bus:/:/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
lizhipeng:x:1000:1000::/home/lizhipeng:/bin/bash
user1:x:1001:1001::/home/user1:/bin/bash
user5:x:1007:1007::/home/user5:/bin/bash

[root@lizhipeng01 awk]# awk -F ':' '$3>"5" && $3<"7"' test.txt      第三段大于字符串5,小于字符串7
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

[root@lizhipeng01 awk]# awk -F ':' '$3>1000 || $7 ~ /bash/' test.txt    第7端匹配bash
root:x:0:0:root:/root:/bin/bash
lizhipeng:x:1000:1000::/home/lizhipeng:/bin/bash
user1:x:1001:1001::/home/user1:/bin/bash
lzp:x:1002:1003::/home/lzp:/bin/bash
user2:x:1003:1006::/home/user2:/bin/bash
user3:x:1004:1005::/home/user3:/bin/bash
user4:x:1006:1005::/home/lizhipeng001:/sbin/nologin
user5:x:1007:1007::/home/user5:/bin/bash

[root@lizhipeng01 awk]# awk -F ':' '{OFS="#"} $3>1000 || $7 ~ /bash/ {print $1,$3,$7}' test.txt       分隔符:,后面紧跟下次输出分隔符的标识#
root#0#/bin/bash
lizhipeng#1000#/bin/bash
user1#1001#/bin/bash
lzp#1002#/bin/bash
user2#1003#/bin/bash
user3#1004#/bin/bash
user4#1006#/sbin/nologin
user5#1007#/bin/bash

[root@lizhipeng01 awk]# awk -F ':' '{OFS="#"} {if ($3>1000) {print $1,$3,$7}}' test.txt
user1#1001#/bin/bash
lzp#1002#/bin/bash
user2#1003#/bin/bash
user3#1004#/bin/bash
user4#1006#/sbin/nologin
user5#1007#/bin/bash

[root@lizhipeng01 awk]# awk -F ':' '{print NR ":" $0}' test.txt         NR打印行号
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
6:sync:x:5:0:sync:/sbin:/bin/sync
7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8:halt:x:7:0:halt:/sbin:/sbin/halt
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
11:games:x:12:100:games:/usr/games:/sbin/nologin
12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13:nobody:x:99:99:Nobody:/:/sbin/nologin
14:avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
15:dbus:x:81:81:System message bus:/:/sbin/nologin
16:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
17:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
18:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
20:lizhipeng:x:1000:1000::/home/lizhipeng:/bin/bash
21:user1:x:1001:1001::/home/user1:/bin/bash
22:lzp:x:1002:1003::/home/lzp:/bin/bash
23:user2:x:1003:1006::/home/user2:/bin/bash
24:user3:x:1004:1005::/home/user3:/bin/bash
25:user4:x:1006:1005::/home/lizhipeng001:/sbin/nologin
26:user5:x:1007:1007::/home/user5:/bin/bash

[root@lizhipeng01 awk]# awk -F ':' '{print NF ":" $0}' test.txt                查看有多少段,都是7段
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
7:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
7:sync:x:5:0:sync:/sbin:/bin/sync
7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
7:halt:x:7:0:halt:/sbin:/sbin/halt
7:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
7:operator:x:11:0:operator:/root:/sbin/nologin
7:games:x:12:100:games:/usr/games:/sbin/nologin
7:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
7:nobody:x:99:99:Nobody:/:/sbin/nologin
7:avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
7:dbus:x:81:81:System message bus:/:/sbin/nologin
7:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
7:tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
7:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
7:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
7:lizhipeng:x:1000:1000::/home/lizhipeng:/bin/bash
7:user1:x:1001:1001::/home/user1:/bin/bash
7:lzp:x:1002:1003::/home/lzp:/bin/bash
7:user2:x:1003:1006::/home/user2:/bin/bash
7:user3:x:1004:1005::/home/user3:/bin/bash
7:user4:x:1006:1005::/home/lizhipeng001:/sbin/nologin
7:user5:x:1007:1007::/home/user5:/bin/bash

[root@lizhipeng01 awk]# awk -F ':' 'NR<=10' test.txt
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
sync:x:5:0:sync:/sbin:/bin/sync
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
operator:x:11:0:operator:/root:/sbin/nologin
[root@lizhipeng01 awk]# awk -F ':' 'NR<=10 && $1 ~ /root|sync/' test.txt
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync

[root@lizhipeng01 awk]# head -n3 test.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@lizhipeng01 awk]# head -n3 test.txt |awk -F ':' '$1=="root"'
root:x:0:0:root:/root:/bin/bash

[root@lizhipeng01 awk]# head -n3 test.txt |awk -F ':' '{OFS=":"}$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@lizhipeng01 awk]# awk -F ':' '{(tot=tot+$3)};END {print tot}' test.txt     第三段的值相加
8667

 

posted @ 2018-01-15 22:40  sisul  阅读(228)  评论(0编辑  收藏  举报