shell涓夊墤瀹箣awk(鍖呮嫭sort鎺掑簭鍜寀niq宸ュ叿)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell涓夊墤瀹箣awk(鍖呮嫭sort鎺掑簭鍜寀niq宸ュ叿)相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e8%ae%a1%e6%95%b0' title='璁℃暟'>璁℃暟 unix ldo service dbus 閮ㄥ垎 椤哄簭 tac
shell涓夊墤瀹箣awk
鍦?Linux/UNIX 绯荤粺涓紝awk 鏄竴涓姛鑳藉己澶х殑缂栬緫宸ュ叿锛岄€愯璇诲彇杈撳叆鏂囨湰锛屽苟鏍规嵁鎸囧畾鐨勫尮閰嶆ā寮忚繘琛屾煡鎵撅紝瀵圭鍚堟潯浠剁殑鍐呭杩涜鏍煎紡鍖栬緭鍑烘垨鑰呰繃婊ゅ鐞嗭紝鍙互鍦ㄦ棤浜や簰鐨勬儏鍐典笅瀹炵幇鐩稿綋澶嶆潅鐨勬枃鏈搷浣滐紝琚箍娉涘簲鐢ㄤ簬 Shell 鑴氭湰锛屽畬鎴愬悇绉嶈嚜鍔ㄥ寲閰嶇疆浠诲姟銆?/p>
awk 鎵€浣跨敤鐨勫懡浠ゆ牸寮忓涓嬫墍绀猴紝鍏朵腑锛屽崟寮曞彿鍔犱笂澶ф嫭鍙封€渰}鈥濈敤浜庤缃鏁版嵁杩涜鐨勫鐞嗗姩浣溿€俛wk 鍙互鐩存帴澶勭悊鐩爣鏂囦欢锛屼篃鍙互閫氳繃鈥?f鈥濊鍙栬剼鏈鐩爣鏂囦欢杩涜澶勭悊銆?/p>
awk宸ュ叿鍛戒护鏍煎紡
awk 閫夐」 鈥樻ā寮忔垨鏉′欢 {缂栬緫鎸囦护}鈥?鏂囦欢 1 鏂囦欢 2 ? //杩囨护骞惰緭鍑烘枃浠剁鏉′欢鐨勫唴瀹?awk -f 鑴氭湰鏂囦欢 鏂囦欢 1 鏂囦欢 2 ? //浠庤剼鏈腑璋冪敤缂栬緫鎸囦护,杩囨护骞惰緭鍑哄唴瀹?7
awk 姣旇緝鍊惧悜浜庡皢涓€琛屽垎鎴愬涓€滃瓧娈碘€濈劧鍚庡啀杩涜澶勭悊锛屼笖榛樿鎯呭喌涓嬪瓧娈电殑鍒嗛殧绗︿负绌烘牸鎴栬€?tab 閿€俛wk 鎵ц缁撴灉鍙互閫氳繃 print 鐨勫姛鑳藉皢瀛楁鏁版嵁鎵撳嵃鏄剧ず銆傚湪浣跨敤 awk 鍛戒护鐨勮繃绋嬩腑,鍙互浣跨敤閫昏緫鎿嶄綔绗︹€?amp;&鈥濓紝琛ㄧず鈥滀笌鈥濓紝 鈥渱|鈥濊〃绀衡€滄垨鈥濓紝鈥滐紒鈥濊〃绀衡€滈潪鈥濓紱杩樺彲浠ヨ繘琛岀畝鍗曠殑鏁板杩愮畻锛屽+銆?銆?銆?銆?銆乛鍒嗗埆 琛ㄧず鍔犮€佸噺銆佷箻銆侀櫎銆佸彇浣欏拰涔樻柟銆?br/>鎵惧嚭/etc/passwd 鐨勭敤鎴峰悕銆佺敤鎴?ID銆佺粍 ID 绛夊垪锛?鎵ц浠ヤ笅 awk 鍛戒护鍗冲彲銆?/p>
[root@localhost ~]# awk -F : 鈥榹print $1,$3,$4}鈥?/etc/passwd
root 0 0
bin 1 1
daemon 2 2
adm 3 4
lp 4 7
sync 5 0
shutdown 6 0
halt 7 0
mail 8 12
operator 11 0
games 12 100
ftp 14 50
nobody 99 99
systemd-network 192 192
dbus 81 81
polkitd 999 998
abrt 173 173
libstoragemgmt 998 996
rpc 32 32
colord 997 995
saslauth 996 76
rtkit 172 172
pulse 171 171
chrony 995 991
rpcuser 29 29
nfsnobody 65534 65534
ntp 38 38
tss 59 59
usbmuxd 113 113
geoclue 994 989
qemu 107 107
radvd 75 75
setroubleshoot 993 988
sssd 992 987
gdm 42 42
gnome-initial-setup 991 986
sshd 74 74
avahi 70 70
postfix 89 89
tcpdump 72 72
chen 1000 1000
awk 浠庤緭鍏ユ枃浠舵垨鑰呮爣鍑嗚緭鍏ヤ腑璇诲叆淇℃伅锛屼笌 sed 涓€鏍凤紝淇℃伅鐨勮鍏ヤ篃鏄€愯璇诲彇鐨勩€備笉鍚岀殑鏄?awk 灏嗘枃鏈枃浠朵腑鐨勪竴琛岃涓轰竴涓褰曪紝鑰屽皢涓€琛屼腑鐨勬煇涓€閮ㄥ垎锛堝垪锛変綔涓鸿褰曚腑鐨勪竴涓瓧娈碉紙鍩燂級銆備负浜嗘搷浣滆繖浜涗笉鍚岀殑瀛楁锛宎wk 鍊熺敤 shell 涓被浼间簬浣嶇疆鍙橀噺鐨勬柟娉曪紝 鐢?1銆?2銆?3?椤哄簭鍦拌〃绀鸿锛堣褰曪級涓殑涓嶅悓瀛楁銆傚彟澶?awk 鐢?0 琛ㄧず鏁翠釜琛岋紙璁板綍锛夈€備笉鍚岀殑瀛楁涔嬮棿鏄€氳繃鎸囧畾鐨勫瓧绗﹀垎闅斻€俛wk 榛樿鐨勫垎闅旂鏄┖鏍笺€俛wk 鍏佽鍦ㄥ懡浠よ涓敤鈥?F 鍒嗛殧绗︹€濈殑褰㈠紡鏉ユ寚瀹氬垎闅旂銆?/p>
awk 鍖呭惈鍑犱釜鐗规畩鐨勫唴寤哄彉閲忥紙鍙洿鎺ョ敤锛夊涓嬫墍绀猴細
锛?锛塅S锛氭寚瀹氭瘡琛屾枃鏈殑瀛楁鍒嗛殧绗︼紝榛樿涓虹┖鏍兼垨鍒惰〃浣嶃€?br/>锛?锛塏F锛氬綋鍓嶅鐞嗙殑琛岀殑瀛楁涓暟銆?br/>锛?锛塏R锛氬綋鍓嶅鐞嗙殑琛岀殑琛屽彿锛堝簭鏁帮級銆?br/>锛?锛?0锛氬綋鍓嶅鐞嗙殑琛岀殑鏁磋鍐呭銆?br/>锛?锛塅ILENAME锛氳澶勭悊鐨勬枃浠跺悕銆?br/>锛?锛塕S9锛氭暟鎹褰曞垎闅旓紝榛樿涓篭n锛屽嵆姣忚涓轰竴鏉¤褰曘€?/p>
涓€.awk鐢ㄦ硶
(1)杈撳嚭鎵€鏈夊唴瀹?绛夊悓浜?cat chen.txt,
[root@localhost ~]# awk 鈥榹print $0}鈥?chen.txt
[root@localhost ~]# awk 鈥榹print}鈥?chen.txt //杩欐潯鍛戒护鍜屼笂闈竴鏉℃槸涓€鏍风殑鏁堟灉
#version=DEVEL
#System authorization information
aulth --enableshadow --passalgo=sha512
#Use CDROM installation media
cdlrom.
thethethe.
(2)杈撳嚭绗?1~3 琛屽唴瀹?/h3>
[root@localhost ~]# awk 鈥楴R==1,NR==3{print}鈥?chen.txt //杩欐潯鍛戒护鍜屼笅闈㈠懡浠ゆ槸涓€鏍风殑鏁堟灉
[root@localhost ~]# awk 鈥?NR>=1)&&(NR<=3){print}鈥?chen.txt
#version=DEVEL
#System authorization information
aulth --enableshadow --passalgo=sha512
(3)杈撳嚭鎵€鏈夊鏁拌鐨勫唴瀹?/h3>
[root@localhost ~]# awk 鈥?NR%2)==1{print}鈥?chen.txt
Use CDROM installation media
cdrom.
thethethe.
THE
THEASDHAS
Use graphical install.
graphical.
best
test
ASSDJFXYxyzC
AxyzxyzxyzC
keyboard --vckeymap=cn --xlayouts=鈥榗n鈥? System language
锛?锛夎緭鍑烘墍鏈夊伓鏁拌鐨勫唴瀹?/h3>
[root@localhost ~]# awk 鈥?NR%2)==0{print}鈥?chen.txt
Use graphical install.
graphical.
best
test
ASSDJFXYxyzC
AxyzxyzxyzC
keyboard --vckeymap=cn --xlayouts=鈥榗n鈥? System language
lang zh_CN.UTF-8
Network information
network --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate
network --hostname=localhost.localdomain
Root password
rootpw --iscrypted $6$L.egxzosoP/0k9Nj$wna7vPXZjeH0jFcNZUymYKF8ySXq5HxQuvxTFxIpEAAxuDj7MQJtXBds5E0LxAftI1H5JbJuYpN44d5n6t1AZ.
System services
(5)杈撳嚭鎵€鏈夊伓鏁拌鐨勫唴瀹?/h3>
awk 鈥?^The/{print}鈥?chen.txt
THE
THEASDHAS
(6)杈撳嚭浠imit.缁撳熬鐨勮
[root@localhost ~]# awk 鈥?limit.$/{print}鈥?chen.txt
Use graphical install limit.
锛?锛夌粺璁′互/bin/bash 缁撳熬鐨勮鏁帮紝绛夊悓浜巊reo -c 鈥?bin/bash鈥?chen.txt
[root@localhost ~]# awk 鈥楤EGIN {x=0} ; //bin/bash$/{x++};END{print x}鈥?/etc/passwd
(8)缁熻浠he鍒嗛殧鐨勬枃鏈钀芥暟
[root@localhost ~]# awk 鈥楤EGIN {RS="the"};END{print NR}鈥?chen.txt
(浜?鎸夊瓧娈佃緭鍑烘枃鏈?/h3>
[root@localhost ~]# awk -F : 鈥榹print $3}鈥?/etc/passwd
0
1
2
3
4
5
6
7
8
9
10
11
锛?锛夎緭鍑烘瘡琛屼腑鐨勭1锛?涓瓧娈?/h3>
[root@localhost ~]# awk -F : 鈥榹print $1,$3}鈥?/etc/passwd
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5
shutdown 6
halt 7
mail 8
operator 11
games 12
ftp 14
nobody 99
systemd-network 192
锛?锛夎緭鍑哄瘑鐮佷负鈥?鈥濈殑鐢ㄦ埛鐨剆hadow 璁板綍
[root@localhost ~]# awk -F : 鈥?2== "*"{print}鈥?/etc/shadow
bin:*:17110:0:99999:7:::
daemon:*:17110:0:99999:7:::
adm:*:17110:0:99999:7:::
lp:*:17110:0:99999:7:::
sync:*:17110:0:99999:7:::
shutdown:*:17110:0:99999:7:::
halt:*:17110:0:99999:7:::
mail:*:17110:0:99999:7:::
operator:*:17110:0:99999:7:::
games:*:17110:0:99999:7:::
锛?锛夎緭鍑轰互鍐掑彿鍒嗛殧涓旂 7 涓瓧娈典腑鍖呭惈/bash 鐨勮鐨勭 1 涓瓧娈?/h3>
[root@localhost ~]# awk 鈥?$1~"nfs")&&(NF=7){print $1,$2}鈥?/etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS
(4)杈撳嚭绗?7 涓瓧娈垫棦涓嶄负/bin/bash 涔熶笉涓?sbin/nologin 鐨勬墍鏈夎
[root@localhost ~]# awk -F : 鈥?$7!="/bin/bash")&&($7!="/sbin/nologin")鈥?/etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
(浜?閫氳繃绠¢亾锛屽弻寮曞彿璋冪敤shell鍛戒护
璋冪敤wc -l 鍛戒护缁熻浣跨敤bash 鐨勭敤鎴蜂釜鏁?绛夊悓浜?grep -c 鈥渂ash$鈥?/etc/passwd
[root@localhost ~]# awk -F : 鈥?bash$/{print | "wc -l"}鈥?/etc/passwd
2
锛?锛夎皟鐢╳ 鍛戒护,骞剁敤鏉ョ粺璁″湪绾跨敤鎴锋暟
[root@localhost ~]# awk 鈥楤EGIN {while ("w" | getline) n++ ;{print n-2}}鈥?2
锛?锛夎皟鐢╤ostname,骞惰緭鍑哄綋鍓嶇殑涓绘満鍚?/h3>
[root@localhost ~]# awk 鈥楤EGIN {"hostname" | getline ; print $0}鈥?localhost.localdomain
涓?sort 宸ュ叿
[root@localhost ~]# awk 鈥楴R==1,NR==3{print}鈥?chen.txt //杩欐潯鍛戒护鍜屼笅闈㈠懡浠ゆ槸涓€鏍风殑鏁堟灉
[root@localhost ~]# awk 鈥?NR>=1)&&(NR<=3){print}鈥?chen.txt
#version=DEVEL
#System authorization information
aulth --enableshadow --passalgo=sha512
[root@localhost ~]# awk 鈥?NR%2)==1{print}鈥?chen.txt
Use CDROM installation media
cdrom.
thethethe.
THE
THEASDHAS
Use graphical install.
graphical.
best
test
ASSDJFXYxyzC
AxyzxyzxyzC
keyboard --vckeymap=cn --xlayouts=鈥榗n鈥? System language
锛?锛夎緭鍑烘墍鏈夊伓鏁拌鐨勫唴瀹?/h3>
[root@localhost ~]# awk 鈥?NR%2)==0{print}鈥?chen.txt
Use graphical install.
graphical.
best
test
ASSDJFXYxyzC
AxyzxyzxyzC
keyboard --vckeymap=cn --xlayouts=鈥榗n鈥? System language
lang zh_CN.UTF-8
Network information
network --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate
network --hostname=localhost.localdomain
Root password
rootpw --iscrypted $6$L.egxzosoP/0k9Nj$wna7vPXZjeH0jFcNZUymYKF8ySXq5HxQuvxTFxIpEAAxuDj7MQJtXBds5E0LxAftI1H5JbJuYpN44d5n6t1AZ.
System services
(5)杈撳嚭鎵€鏈夊伓鏁拌鐨勫唴瀹?/h3>
awk 鈥?^The/{print}鈥?chen.txt
THE
THEASDHAS
(6)杈撳嚭浠imit.缁撳熬鐨勮
[root@localhost ~]# awk 鈥?limit.$/{print}鈥?chen.txt
Use graphical install limit.
锛?锛夌粺璁′互/bin/bash 缁撳熬鐨勮鏁帮紝绛夊悓浜巊reo -c 鈥?bin/bash鈥?chen.txt
[root@localhost ~]# awk 鈥楤EGIN {x=0} ; //bin/bash$/{x++};END{print x}鈥?/etc/passwd
(8)缁熻浠he鍒嗛殧鐨勬枃鏈钀芥暟
[root@localhost ~]# awk 鈥楤EGIN {RS="the"};END{print NR}鈥?chen.txt
(浜?鎸夊瓧娈佃緭鍑烘枃鏈?/h3>
[root@localhost ~]# awk -F : 鈥榹print $3}鈥?/etc/passwd
0
1
2
3
4
5
6
7
8
9
10
11
锛?锛夎緭鍑烘瘡琛屼腑鐨勭1锛?涓瓧娈?/h3>
[root@localhost ~]# awk -F : 鈥榹print $1,$3}鈥?/etc/passwd
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5
shutdown 6
halt 7
mail 8
operator 11
games 12
ftp 14
nobody 99
systemd-network 192
锛?锛夎緭鍑哄瘑鐮佷负鈥?鈥濈殑鐢ㄦ埛鐨剆hadow 璁板綍
[root@localhost ~]# awk -F : 鈥?2== "*"{print}鈥?/etc/shadow
bin:*:17110:0:99999:7:::
daemon:*:17110:0:99999:7:::
adm:*:17110:0:99999:7:::
lp:*:17110:0:99999:7:::
sync:*:17110:0:99999:7:::
shutdown:*:17110:0:99999:7:::
halt:*:17110:0:99999:7:::
mail:*:17110:0:99999:7:::
operator:*:17110:0:99999:7:::
games:*:17110:0:99999:7:::
锛?锛夎緭鍑轰互鍐掑彿鍒嗛殧涓旂 7 涓瓧娈典腑鍖呭惈/bash 鐨勮鐨勭 1 涓瓧娈?/h3>
[root@localhost ~]# awk 鈥?$1~"nfs")&&(NF=7){print $1,$2}鈥?/etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS
(4)杈撳嚭绗?7 涓瓧娈垫棦涓嶄负/bin/bash 涔熶笉涓?sbin/nologin 鐨勬墍鏈夎
[root@localhost ~]# awk -F : 鈥?$7!="/bin/bash")&&($7!="/sbin/nologin")鈥?/etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
(浜?閫氳繃绠¢亾锛屽弻寮曞彿璋冪敤shell鍛戒护
璋冪敤wc -l 鍛戒护缁熻浣跨敤bash 鐨勭敤鎴蜂釜鏁?绛夊悓浜?grep -c 鈥渂ash$鈥?/etc/passwd
[root@localhost ~]# awk -F : 鈥?bash$/{print | "wc -l"}鈥?/etc/passwd
2
锛?锛夎皟鐢╳ 鍛戒护,骞剁敤鏉ョ粺璁″湪绾跨敤鎴锋暟
[root@localhost ~]# awk 鈥楤EGIN {while ("w" | getline) n++ ;{print n-2}}鈥?2
锛?锛夎皟鐢╤ostname,骞惰緭鍑哄綋鍓嶇殑涓绘満鍚?/h3>
[root@localhost ~]# awk 鈥楤EGIN {"hostname" | getline ; print $0}鈥?localhost.localdomain
涓?sort 宸ュ叿
[root@localhost ~]# awk 鈥?NR%2)==0{print}鈥?chen.txt
Use graphical install.
graphical.
best
test
ASSDJFXYxyzC
AxyzxyzxyzC
keyboard --vckeymap=cn --xlayouts=鈥榗n鈥? System language
lang zh_CN.UTF-8
Network information
network --bootproto=dhcp --device=ens33 --onboot=off --ipv6=auto --no-activate
network --hostname=localhost.localdomain
Root password
rootpw --iscrypted $6$L.egxzosoP/0k9Nj$wna7vPXZjeH0jFcNZUymYKF8ySXq5HxQuvxTFxIpEAAxuDj7MQJtXBds5E0LxAftI1H5JbJuYpN44d5n6t1AZ.
System services
awk 鈥?^The/{print}鈥?chen.txt
THE
THEASDHAS
(6)杈撳嚭浠imit.缁撳熬鐨勮
[root@localhost ~]# awk 鈥?limit.$/{print}鈥?chen.txt
Use graphical install limit.
锛?锛夌粺璁′互/bin/bash 缁撳熬鐨勮鏁帮紝绛夊悓浜巊reo -c 鈥?bin/bash鈥?chen.txt
[root@localhost ~]# awk 鈥楤EGIN {x=0} ; //bin/bash$/{x++};END{print x}鈥?/etc/passwd
(8)缁熻浠he鍒嗛殧鐨勬枃鏈钀芥暟
[root@localhost ~]# awk 鈥楤EGIN {RS="the"};END{print NR}鈥?chen.txt
(浜?鎸夊瓧娈佃緭鍑烘枃鏈?/h3>
[root@localhost ~]# awk -F : 鈥榹print $3}鈥?/etc/passwd
0
1
2
3
4
5
6
7
8
9
10
11
锛?锛夎緭鍑烘瘡琛屼腑鐨勭1锛?涓瓧娈?/h3>
[root@localhost ~]# awk -F : 鈥榹print $1,$3}鈥?/etc/passwd
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5
shutdown 6
halt 7
mail 8
operator 11
games 12
ftp 14
nobody 99
systemd-network 192
锛?锛夎緭鍑哄瘑鐮佷负鈥?鈥濈殑鐢ㄦ埛鐨剆hadow 璁板綍
[root@localhost ~]# awk -F : 鈥?2== "*"{print}鈥?/etc/shadow
bin:*:17110:0:99999:7:::
daemon:*:17110:0:99999:7:::
adm:*:17110:0:99999:7:::
lp:*:17110:0:99999:7:::
sync:*:17110:0:99999:7:::
shutdown:*:17110:0:99999:7:::
halt:*:17110:0:99999:7:::
mail:*:17110:0:99999:7:::
operator:*:17110:0:99999:7:::
games:*:17110:0:99999:7:::
锛?锛夎緭鍑轰互鍐掑彿鍒嗛殧涓旂 7 涓瓧娈典腑鍖呭惈/bash 鐨勮鐨勭 1 涓瓧娈?/h3>
[root@localhost ~]# awk 鈥?$1~"nfs")&&(NF=7){print $1,$2}鈥?/etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS
(4)杈撳嚭绗?7 涓瓧娈垫棦涓嶄负/bin/bash 涔熶笉涓?sbin/nologin 鐨勬墍鏈夎
[root@localhost ~]# awk -F : 鈥?$7!="/bin/bash")&&($7!="/sbin/nologin")鈥?/etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
(浜?閫氳繃绠¢亾锛屽弻寮曞彿璋冪敤shell鍛戒护
璋冪敤wc -l 鍛戒护缁熻浣跨敤bash 鐨勭敤鎴蜂釜鏁?绛夊悓浜?grep -c 鈥渂ash$鈥?/etc/passwd
[root@localhost ~]# awk -F : 鈥?bash$/{print | "wc -l"}鈥?/etc/passwd
2
锛?锛夎皟鐢╳ 鍛戒护,骞剁敤鏉ョ粺璁″湪绾跨敤鎴锋暟
[root@localhost ~]# awk 鈥楤EGIN {while ("w" | getline) n++ ;{print n-2}}鈥?2
锛?锛夎皟鐢╤ostname,骞惰緭鍑哄綋鍓嶇殑涓绘満鍚?/h3>
[root@localhost ~]# awk 鈥楤EGIN {"hostname" | getline ; print $0}鈥?localhost.localdomain
涓?sort 宸ュ叿
[root@localhost ~]# awk -F : 鈥榹print $3}鈥?/etc/passwd
0
1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# awk -F : 鈥榹print $1,$3}鈥?/etc/passwd
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5
shutdown 6
halt 7
mail 8
operator 11
games 12
ftp 14
nobody 99
systemd-network 192
锛?锛夎緭鍑哄瘑鐮佷负鈥?鈥濈殑鐢ㄦ埛鐨剆hadow 璁板綍
[root@localhost ~]# awk -F : 鈥?2== "*"{print}鈥?/etc/shadow
bin:*:17110:0:99999:7:::
daemon:*:17110:0:99999:7:::
adm:*:17110:0:99999:7:::
lp:*:17110:0:99999:7:::
sync:*:17110:0:99999:7:::
shutdown:*:17110:0:99999:7:::
halt:*:17110:0:99999:7:::
mail:*:17110:0:99999:7:::
operator:*:17110:0:99999:7:::
games:*:17110:0:99999:7:::
锛?锛夎緭鍑轰互鍐掑彿鍒嗛殧涓旂 7 涓瓧娈典腑鍖呭惈/bash 鐨勮鐨勭 1 涓瓧娈?/h3>
[root@localhost ~]# awk 鈥?$1~"nfs")&&(NF=7){print $1,$2}鈥?/etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS
(4)杈撳嚭绗?7 涓瓧娈垫棦涓嶄负/bin/bash 涔熶笉涓?sbin/nologin 鐨勬墍鏈夎
[root@localhost ~]# awk -F : 鈥?$7!="/bin/bash")&&($7!="/sbin/nologin")鈥?/etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
(浜?閫氳繃绠¢亾锛屽弻寮曞彿璋冪敤shell鍛戒护
璋冪敤wc -l 鍛戒护缁熻浣跨敤bash 鐨勭敤鎴蜂釜鏁?绛夊悓浜?grep -c 鈥渂ash$鈥?/etc/passwd
[root@localhost ~]# awk -F : 鈥?bash$/{print | "wc -l"}鈥?/etc/passwd
2
锛?锛夎皟鐢╳ 鍛戒护,骞剁敤鏉ョ粺璁″湪绾跨敤鎴锋暟
[root@localhost ~]# awk 鈥楤EGIN {while ("w" | getline) n++ ;{print n-2}}鈥?2
锛?锛夎皟鐢╤ostname,骞惰緭鍑哄綋鍓嶇殑涓绘満鍚?/h3>
[root@localhost ~]# awk 鈥楤EGIN {"hostname" | getline ; print $0}鈥?localhost.localdomain
涓?sort 宸ュ叿
[root@localhost ~]# awk 鈥?$1~"nfs")&&(NF=7){print $1,$2}鈥?/etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS
[root@localhost ~]# awk -F : 鈥?$7!="/bin/bash")&&($7!="/sbin/nologin")鈥?/etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
[root@localhost ~]# awk -F : 鈥?bash$/{print | "wc -l"}鈥?/etc/passwd
2
[root@localhost ~]# awk 鈥楤EGIN {while ("w" | getline) n++ ;{print n-2}}鈥?2
[root@localhost ~]# awk 鈥楤EGIN {"hostname" | getline ; print $0}鈥?localhost.localdomain
涓?sort 宸ュ叿
sort 鏄竴涓互琛屼负鍗曚綅瀵规枃浠跺唴瀹硅繘琛屾帓搴忕殑宸ュ叿锛屼篃鍙互鏍规嵁涓嶅悓鐨勬暟鎹被鍨嬫潵鎺掑簭銆備緥濡傛暟鎹拰瀛楃鐨勭墝灞€灏变笉涓€鏍枫€俿ort 鍛戒护鐨勮娉曚负鈥渟ort [閫夐」] 鍙傛暟鈥濓紝鍏朵腑甯哥敤鐨勯€夐」鍖呮嫭浠ヤ笅鍑犵銆?br/>锛?锛?f锛氬拷鐣ュぇ灏忓啓锛?br/>锛?锛?b锛氬拷鐣ユ瘡琛屽墠闈㈢殑绌烘牸锛?br/>锛?锛?M锛氭寜鐓ф湀浠借繘琛屾帓搴忥紱
锛?锛?n锛氭寜鐓ф暟瀛楄繘琛屾帓搴忥紱
锛?锛?r锛氬弽鍚戞帓搴忥紱
锛?锛?u锛氱瓑鍚屼簬 uniq锛岃〃绀虹浉鍚岀殑鏁版嵁浠呮樉绀轰竴琛岋紱
锛?锛?t锛氭寚瀹氬垎闅旂锛岄粯璁や娇鐢╗Tab]閿垎闅旓紱
锛?锛?o <杈撳嚭鏂囦欢>锛氬皢鎺掑簭鍚庣殑缁撴灉杞瓨鑷虫寚瀹氭枃浠讹紱
锛?锛?k锛氭寚瀹氭帓搴忓尯鍩熴€?/p>
1锛氬皢/etc/passwd 鏂囦欢涓殑璐﹀彿杩涜鎺掑簭銆?/h3>
鎺掑簭瑙勫垯鏄紑澶存寜瀛楁瘝鎺掑簭锛屽鏋滃紑澶撮兘鏄竴鏍风殑灏辨寜鐓х浜屼釜瀛楁瘝浠庡皬鍒板ぇ
[root@localhost ~]# sort /etc/passwd
abrt:x:173:173::/etc/abrt:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
chen:x:1000:1000:chen:/home/chen:/bin/bash
chrony:x:995:991::/var/lib/chrony:/sbin/nologin
colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
锛?锛夊皢/etc/passwd 鏂囦欢涓涓夊垪杩涜鍙嶅悜鎺掑簭
[root@localhost ~]# sort -t : -rk 3 /etc/passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
libstoragemgmt:x:998:996:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
chrony:x:995:991::/var/lib/chrony:/sbin/nologin
geoclue:x:994:989:User for geoclue:/var/lib/geoclue:/sbin/nologin
setroubleshoot:x:993:988::/var/lib/setroubleshoot:/sbin/nologin
sssd:x:992:987:User for sssd:/:/sbin/nologin
gnome-initial-setup:x:991:986::/run/gnome-initial-setup/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
锛?锛夊皢/etc/passwd 鏂囦欢涓涓夊垪杩涜鎺掑簭锛屽苟灏嗚緭鍑哄唴瀹逛繚瀛樿嚦user.txt 鏂囦欢涓?/h3>
[root@localhost ~]# sort -t : -k 3 /etc/passwd -o chench.txt
[root@localhost ~]# cat chench.txt
root:x:0:0:root:/root:/bin/bash
chen:x:1000:1000:chen:/home/chen:/bin/bash
qemu:x:107:107:qemu user:/:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbi
锛堝洓锛塽niq宸ュ叿
[root@localhost ~]# sort -t : -k 3 /etc/passwd -o chench.txt
[root@localhost ~]# cat chench.txt
root:x:0:0:root:/root:/bin/bash
chen:x:1000:1000:chen:/home/chen:/bin/bash
qemu:x:107:107:qemu user:/:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbi
Uniq 宸ュ叿鍦?Linux 绯荤粺涓€氬父涓?sort 鍛戒护缁撳悎浣跨敤锛岀敤浜庢姤鍛婃垨鑰呭拷鐣ユ枃浠朵腑鐨勯噸澶嶈銆傚叿浣撶殑鍛戒护璇硶鏍煎紡涓猴細uniq [閫夐」] 鍙傛暟銆傚叾涓父鐢ㄩ€夐」鍖呮嫭浠ヤ笅鍑犵銆?br/>锛?锛?c锛氳繘琛岃鏁帮紱
锛?锛?d锛氫粎鏄剧ず閲嶅琛岋紱
锛?锛?u锛氫粎鏄剧ず鍑虹幇涓€娆$殑琛岋紱
1锛氬垹闄?a 鏂囦欢涓殑閲嶅琛屻€?/h3>
[root@localhost ~]# cat a.txt
centos7 6.2
centos7 6.2
centos7 6.2
centos7 6.2
centos7 6.2
centos7 6.6
centos7 6.2
centos7 6.2
centos7 6.3
centos7 6.5
linux 1
linux 2
linux 3
linux 4
linux 5
linux 6
[root@localhost ~]# uniq a.txt
centos7 6.2
centos7 6.6
centos7 6.2
centos7 6.3
centos7 6.5
linux 1
linux 2
linux 3
linux 4
linux 5
linux 6
uniq宸ュ叿鍒犻櫎閲嶅浣嗘槸鍙垹闄よ繛缁噸澶嶇殑
锛?锛夊垹闄?a 鏂囦欢涓殑閲嶅琛岋紝骞跺湪琛岄鏄剧ず璇ヨ閲嶅鍑虹幇鐨勬鏁?/h3>
[root@localhost ~]# uniq -c a.txt
1
5 centos7 6.2
1 centos7 6.6
2 centos7 6.2
1 centos7 6.3
1 centos7 6.5
1 linux 1
1 linux 2
1 linux 3
1 linux 4
1 linux 5
1 linux 6
1
(3)鏌ユ壘 testfile 鏂囦欢涓殑閲嶅琛屻€?/h3>
[root@localhost ~]# uniq -d a.txt
centos7 6.2
centos7 6.2
璋㈣阿鏀剁湅
[root@localhost ~]# cat a.txt
centos7 6.2
centos7 6.2
centos7 6.2
centos7 6.2
centos7 6.2
centos7 6.6
centos7 6.2
centos7 6.2
centos7 6.3
centos7 6.5
linux 1
linux 2
linux 3
linux 4
linux 5
linux 6
[root@localhost ~]# uniq a.txt
centos7 6.2
centos7 6.6
centos7 6.2
centos7 6.3
centos7 6.5
linux 1
linux 2
linux 3
linux 4
linux 5
linux 6
[root@localhost ~]# uniq -c a.txt
1
5 centos7 6.2
1 centos7 6.6
2 centos7 6.2
1 centos7 6.3
1 centos7 6.5
1 linux 1
1 linux 2
1 linux 3
1 linux 4
1 linux 5
1 linux 6
1
(3)鏌ユ壘 testfile 鏂囦欢涓殑閲嶅琛屻€?/h3>
[root@localhost ~]# uniq -d a.txt
centos7 6.2
centos7 6.2
璋㈣阿鏀剁湅
[root@localhost ~]# uniq -d a.txt
centos7 6.2
centos7 6.2
以上是关于shell涓夊墤瀹箣awk(鍖呮嫭sort鎺掑簭鍜寀niq宸ュ叿)的主要内容,如果未能解决你的问题,请参考以下文章