本篇主要介紹linux下經常使用的增刪改查工具:python
grep是linux下一個強大的搜索工具,幾乎操做linux的用戶天天都會或多或少的用到grep命令,單一個功能再強大,也存在短板,grep最明顯的短板就是不能對已知結果進行更改,所以sed和awk工具就彌補了grep的短板,有人習慣稱grep,sed和awk並稱爲linux下三劍客,是體現了三個工具一個計算機工做者不可缺乏的必備技能之一mysql
♣grep的語法:linux
grep [-abcEFGhHilLnqrsvVwxy][-A<顯示列數>][-B<顯示列數>][-C<顯示列數>][-d<進行動做>][-e<範本樣式>][-f<範本文件>][--help][範本樣式][文件或目錄...]
grep參數使用:nginx
因參數過多,我的將列舉部分經常使用的參數正則表達式
[root@aly-centos7 /]# ps -ef | grep nginx root 14475 14272 0 13:21 pts/0 00:00:00 grep --color=auto nginx [root@aly-centos7 /]# ps -ef | grep -c nginx 1
文件內容: [root@aly-centos7 /]# cat passwd1 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 systemd-bus-proxy:x:999:998:systemd Bus Proxy:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:998:997:User for polkitd:/:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin [root@aly-centos7 /]# cat passwd2 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@aly-centos7 /]#cat passwd3 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 operator:x:11:0:operator:/root:/sbin/nologin 過濾結果: [root@aly-centos7 /]# cat passwd1 | grep -f passwd2 | grep -f passwd3 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 operator:x:11:0:operator:/root:/sbin/nologin [root@aly-centos7 /]# 最後結果顯示三個文件相同的部分
[root@aly-centos7 /]# cat passwd2 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@aly-centos7 /]# cat passwd3 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 operator:x:11:0:operator:/root:/sbin/nologin root,root root123 [root@aly-centos7 /]# grep -n "root" passwd2 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin [root@aly-centos7 /]# grep -n "root" passwd2 passwd3 passwd2:1:root:x:0:0:root:/root:/bin/bash passwd2:10:operator:x:11:0:operator:/root:/sbin/nologin passwd3:1:root:x:0:0:root:/root:/bin/bash passwd3:5:operator:x:11:0:operator:/root:/sbin/nologin passwd3:6:root,root passwd3:7:root123 [root@aly-centos7 /]#
[root@aly-centos7 /]# grep "^r" passwd3 #查找指定字符開頭 root:x:0:0:root:/root:/bin/bash root,root root123 [root@aly-centos7 /]# grep "^[^r]" passwd3 #查找非指定字符開頭 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 operator:x:11:0:operator:/root:/sbin/nologin [root@aly-centos7 /]# grep "n$" passwd3 #查找指定結尾 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 operator:x:11:0:operator:/root:/sbin/nologin [root@aly-centos7 /]#
[root@aly-centos7 /]#cat qq.log | grep -c "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" 130205
[root@aly-centos7 /]# grep -r -n "root" /etc/ /etc/logrotate.d/ppp:9: create 0600 root root /etc/logrotate.d/mysql:9:# In case the root user has a password, then you /etc/logrotate.d/mysql:10:# have to create a /root/.my.cnf configuration file /etc/logrotate.d/mysql:15:# user= root /etc/logrotate.d/mysql:19:# ATTENTION: The /root/.my.cnf file should be readable /etc/logrotate.d/mysql:20:# _ONLY_ by root ! /etc/logrotate.d/mysql-mmm:8: create 640 root adm /etc/logrotate.d/wpa_supplicant:5: create 0600 root root /etc/logrotate.d/yum:6: create 0600 root root /etc/rsyncd.conf:9:# use chroot = yes /etc/statetab:5:# See $STATE_LABEL in /etc/sysconfig/readonly-root /etc/statetab:9:# /root /etc/group:1:root:x:0: /etc/group:49:dockerroot:x:991: /etc/passwd-:1:root:x:0:0:root:/root:/bin/bash /etc/passwd-:10:operator:x:11:0:operator:/root:/sbin/nologin Binary file /etc/aliases.db matches /etc/mime.types:351:application/vnd.cyan.dean.root+xml /etc/mime.types:382:application/vnd.dvb.notif-aggregate-root+xml
grep用於常規的查詢操做當然方便,可是最大的弊端就是查出來不能增刪改,致使若是是寫一些腳本就會很不方便,這個時候就須要sed和awk這樣的工具來實現。sql
♣sed的使用:docker
因參數過多,我的將列舉部分經常使用的參數shell
sed的工做流程:centos
1:sed默認不編輯原文件,而是逐行操做,複製一份到指定內存(pattern space,模式空間)bash
2:pattern space內進行模式匹配,即和指定條件作匹配
不知足模式:輸出到標準輸出STDOUT
知足模式:進行指定的模式操做,再輸出到STDOUT
3:第二個特殊的內存空間 :保持空間(hold space),臨時保存操做在另外一處內存
4:當執行pattern space和 hold space相關選項時候會進行之間的數據流編輯操做
5:最後根據操做執行hold space空間操做,選擇性顯示到STDOUT
查詢:
[root@aly-centos7 /]# sed -n '1,5{/^root/p}' passwd2 root:x:0:0:root:/root:/bin/bash [root@aly-centos7 /]#
[root@aly-centos7 /]# cat passwd3 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 operator:x:11:0:operator:/root:/sbin/nologin root,root root123 [root@aly-centos7 /]# sed -n '/3/,$p' passwd3 #顯示第一次匹配到的3到最行一行輸出 adm:x:3:4:adm:/var/adm:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin root,root root123 [root@aly-centos7 /]# sed -n '/3/,+1p' passwd3#顯示第一次匹配到的3到向下1行輸出 adm:x:3:4:adm:/var/adm:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin root123 [root@aly-centos7 /]#
[root@aly-centos7 /]# cat passwd2 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@aly-centos7 /]# sed '1!G;h;$!d' passwd2 #逆向排序 operator:x:11:0:operator:/root:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin halt:x:7:0:halt:/sbin:/sbin/halt shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown sync:x:5:0:sync:/sbin:/bin/sync lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin root:x:0:0:root:/root:/bin/bash [root@aly-centos7 /]#
operator:x:11:0:operator:/root:/sbin/nologin [root@aly-centos7 /]# [root@aly-centos7 /]# cat passwd2 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@aly-centos7 /]# sed '=' passwd2 #不屏蔽空行 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 11 12 operator:x:11:0:operator:/root:/sbin/nologin [root@aly-centos7 /]# sed '/./=' passwd2 #屏蔽空行 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 12 operator:x:11:0:operator:/root:/sbin/nologin
[root@aly-centos7 /]# grep -n "" passwd3 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:operator:x:11:0:operator:/root:/sbin/nologin 6:root,root 7:root123 [root@aly-centos7 /]# sed -n '$=' passwd3 7 [root@aly-centos7 /]#
[root@aly-centos7 /]# grep -n "" passwd3 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:operator:x:11:0:operator:/root:/sbin/nologin 6:root,root 7:root123 [root@aly-centos7 /]# sed -n 'p;n' passwd3 #奇數行 root:x:0:0:root:/root:/bin/bash daemon:x:2:2:daemon:/sbin:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin root123 [root@aly-centos7 /]# sed -n '1~2p' passwd3 #奇數行 root:x:0:0:root:/root:/bin/bash daemon:x:2:2:daemon:/sbin:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin root123 [root@aly-centos7 /]# sed -n 'n;p' passwd3 #偶數行 bin:x:1:1:bin:/bin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin root,root [root@aly-centos7 /]# sed -n '2~2p' passwd3 #偶數行 bin:x:1:1:bin:/bin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin root,root [root@aly-centos7 /]#
[root@aly-centos7 /]# cat passwd2 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 1234567890 9876543210 [root@aly-centos7 /]# sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' passwd2 hsab/nib/:toor/:toor:0:0:x:toor nigolon/nibs/:nib/:nib:1:1:x:nib nigolon/nibs/:nibs/:nomead:2:2:x:nomead nigolon/nibs/:mda/rav/:mda:4:3:x:mda nigolon/nibs/:dpl/loops/rav/:pl:7:4:x:pl cnys/nib/:nibs/:cnys:0:5:x:cnys nwodtuhs/nibs/:nibs/:nwodtuhs:0:6:x:nwodtuhs% tlah/nibs/:nibs/:tlah:0:7:x:tlah$ nigolon/nibs/:liam/loops/rav/:liam:21:8:x:liam$# nigolon/nibs/:toor/:rotarepo:0:11:x:rotarepo 0987654321 0123456789 [root@aly-centos7 /]#
[root@aly-centos7 /]# cat passwd2 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 1234567890 9876543210 [root@aly-centos7 /]# sed ':a;s/\B[0-9]\{3\}\>/,&/;ta' passwd2 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 1,234,567,890 9,876,543,210 [root@aly-centos7 /]#
刪除:
刪除1-5行內容 [root@aly-centos7 /]# grep -n "" passwd2 | sed '1,5d' 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: 11: 12:operator:x:11:0:operator:/root:/sbin/nologin 13:1234567890 14:9876543210 刪除奇數行: [root@aly-centos7 /]# grep -n "" passwd2 | sed '1~2d' 2:bin:x:1:1:bin:/bin:/sbin/nologin 4:adm:x:3:4:adm:/var/adm:/sbin/nologin 6:sync:x:5:0:sync:/sbin:/bin/sync 8:$halt:x:7:0:halt:/sbin:/sbin/halt 10: 12:operator:x:11:0:operator:/root:/sbin/nologin 14:9876543210 刪除空行 [root@aly-centos7 /]# grep -n "" passwd2 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: 11: 12:operator:x:11:0:operator:/root:/sbin/nologin 13:1234567890 14:9876543210 [root@aly-centos7 /]# grep "" passwd2 | sed '/^$/d' 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 1234567890 9876543210 [root@aly-centos7 /]#
修改:
將root所有替換成mysql [root@aly-centos7 /]# grep -n "" passwd3 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:operator:x:11:0:operator:/root:/sbin/nologin 6:root,root 7:root123 [root@aly-centos7 /]# sed 's/root/mysql/g' passwd3 mysql:x:0:0:mysql:/mysql:/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 operator:x:11:0:operator:/mysql:/sbin/nologin mysql,mysql mysql123 把數字1所有替換成9 [root@aly-centos7 /]# sed 's/1/9/g' passwd3 root:x:0:0:root:/root:/bin/bash bin:x:9:9:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin operator:x:99:0:operator:/root:/sbin/nologin root,root root923 只替換行中未出現「sbin」字符的時候,將」root「替換成」nginx「 [root@aly-centos7 /]# sed '/sbin/!s/root/nginx/g' passwd3 nginx:x:0:0:nginx:/nginx:/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 operator:x:11:0:operator:/root:/sbin/nologin nginx,nginx nginx123 root:x:11:0:operator:/root:/sbin/nologin root1:x:11:0:operator:/root:/sbin/nologin root2:x:11:0:operator:/root:/sbin/nologin [root@aly-centos7 /]#
awk的使用:
在某些場景下,咱們須要過濾方式但願是列來匹配,而不是sed的行來匹配,並且awk還能夠嵌套for等循環去使用,拓展性強,固然awk也是最難的。
awk的經常使用命令選項:
awk內置變量:
查詢:
[root@aly-centos7 /]# awk '{print $0}' passwd4 #$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 1234 567890 9876 543210 [root@aly-centos7 /]# awk '{print $1}' passwd4 #$1表明第一列 root bin daemon adm lp sync %shutdown $halt:x:7:0:halt:/sbin:/sbin/halt #$mail operator 1234 9876 [root@aly-centos7 /]#
首先要查看剩餘內存是那個字段標記,而後標記的這一行用grep取出來,最後用awk把這一行的列取出來在加上一個可讀性語句便可 [root@aly-centos7 /]# free | grep Mem | awk '{print"當前剩餘內存:\n",$7}' 當前剩餘內存: 1408424 [root@aly-centos7 /]#
/var/log/secure是用於記錄訪問的信息,能夠經過這個日誌來看出來是否遭受到惡意攻擊 [root@aly-centos7 /]# grep "Accepted" /var/log/secure | awk '{print $11}' 101.95.130.134 101.95.130.134 101.95.130.134 101.95.130.134 101.95.130.134 101.95.130.134 101.95.130.134 101.95.130.134 [root@aly-centos7 /]# 經過過濾我就知道那些ip訪問了,不過訪問的ip數量不少,而生產服務器訪問的ip都是固定的,通常都會有一個文件記錄,這樣能夠寫一個腳本和你過濾的ip比對,那些沒有登記的就能夠處理掉。
[root@aly-centos7 /]# awk '{print NF}' passwd4 2 2 2 2 2 2 2 1 2 2 2 2 [root@aly-centos7 /]# cat passwd4 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 1234 567890 9876 543210 [root@aly-centos7 /]#
[root@aly-centos7 /]# awk '{print FNR}' passwd3 passwd4 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 11 12 [root@aly-centos7 /]#
[root@aly-centos7 /]# awk -F: 'BEGIN{i=0;j=0}{if($3<=30){i++}else{j++}}END{print "<=30:"i,"\n",">=30:"j}' /etc/passwd <=30:13 >=30:17 [root@aly-centos7 /]#
[root@aly-centos7 /]# grep --color root /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin dockerroot:x:993:991:Docker User:/var/lib/docker:/sbin/nologin [root@aly-centos7 /]# awk -F[:/] '{i=1}{while(i<=NF){if($i~/root/){j++};i++}}END{print j}' /etc/passwd 5 [root@aly-centos7 /]#
[root@aly-centos7 httpd]# tail -10 access_log 172.16.163.69 - - [24/Jul/2018:13:39:12 +0800] "GET / HTTP/1.0" 403 4897 "-" "ApacheBench/2.3" 172.16.163.69 - - [24/Jul/2018:13:39:12 +0800] "GET / HTTP/1.0" 403 4897 "-" "ApacheBench/2.3" 172.16.163.69 - - [24/Jul/2018:13:39:12 +0800] "GET / HTTP/1.0" 403 4897 "-" "ApacheBench/2.3" 172.16.163.69 - - [24/Jul/2018:13:39:12 +0800] "GET / HTTP/1.0" 403 4897 "-" "ApacheBench/2.3" 172.16.163.69 - - [24/Jul/2018:13:39:12 +0800] "GET / HTTP/1.0" 403 4897 "-" "ApacheBench/2.3" 172.16.163.69 - - [24/Jul/2018:13:39:12 +0800] "GET / HTTP/1.0" 403 4897 "-" "ApacheBench/2.3" 172.16.163.69 - - [24/Jul/2018:13:39:12 +0800] "GET / HTTP/1.0" 403 4897 "-" "ApacheBench/2.3" 172.16.163.69 - - [24/Jul/2018:13:39:12 +0800] "GET / HTTP/1.0" 403 4897 "-" "ApacheBench/2.3" 172.16.163.69 - - [24/Jul/2018:13:39:12 +0800] "GET / HTTP/1.0" 403 4897 "-" "ApacheBench/2.3" 172.16.163.69 - - [24/Jul/2018:13:39:12 +0800] "GET / HTTP/1.0" 403 4897 "-" "ApacheBench/2.3" [root@aly-centos7 httpd]# awk '{ip[$1]++} END{for(i in ip){print i,ip[i]}}' /var/log/httpd/access_log 172.16.163.69 1000 [root@aly-centos7 httpd]#
後續將不斷完善awk使用案例