Awk、Sed、Grep、Find。 Grep 文本過濾,會給過濾出來的內容加上顏色。 centos6須要本身設置別名,centos7不須要 選項: -n #給過濾出來的內容加上當前內容所在的行號。 -v #取反,排除 -E #支持擴展正則使用 -i #忽略大小寫 -o #只顯示匹配的內容 -w #只匹配關鍵字(一整串的字符串) -c #統計過濾出來的內容行數 -A #顯示匹配內容的向下的行數 -B #向上匹配 -C #上下匹配 -r #遞歸過濾 -R #遞歸過濾
[root@oldboyedu ~]# grep -n 'root' passwd 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin
[root@oldboyedu ~]# echo Root >> passwd [root@oldboyedu ~]# grep 'root' passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@oldboyedu ~]# grep -i 'root' passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin Root
[root@oldboyedu ~]# grep -v '/sbin/nologin' passwd root:x:0:0:root:/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 Root
[root@oldboyedu ~]# echo roott >>passwd [root@oldboyedu ~]# grep 'root' passwd |wc -l 3 [root@oldboyedu ~]# grep 'root' passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin roott [root@oldboyedu ~]# grep -w 'root' passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
[root@oldboyedu ~]# grep -c 'root' passwd 3 [root@oldboyedu ~]# grep -c 'root' passwd |wc -l 1 [root@oldboyedu ~]# grep 'root' passwd |wc -l 3 [root@oldboyedu ~]# grep -c '.' passwd 38 [root@oldboyedu ~]# wc -l passwd 38 passwd [root@oldboyedu ~]# grep -R 'root' ./ ./.bash_history:cd /root/nginx-1.16.0 ./.bash_history:fpm -s dir -t rpm -v 1.16.0 -n nginx -d 'pcre-devel,openssl-devel,zlib-devel' --post-install /root/nginx.sh -f /app/nginx.1.16.0/ ./.bash_history:fpm -s dir -t rpm -v 1.16.0 -n nginx -d 'pcre-devel,openssl-devel,zlib-devel' --post-install /root/nginx.sh -f /app/nginx.1.16.0/ ./.bash_history:mv /root/nginx-1.16.0-1.x86_64.rpm . ./.bash_history:top -d 1 -u root ./.bash_history:pgrep -u root ./.bash_history:pgrep -u -l root ./.bash_history:pgrep -l -u root ./.bash_history:top -d 1 -u root ./.bash_history:wget [root@oldboyedu ~]# kill 9461 ./.bash_history:mkpasswd -l 20 |passwd --stdin root Binary file ./.gem/specs/mirrors.huaweicloud.com%443/repository/rubygems/specs.4.8 matches Binary file ./.gem/specs/mirrors.huaweicloud.com%443/repository/rubygems/latest_specs.4.8 matches Binary file ./.file2.swp matches Binary file ./sysstat-11.7.3-1.x86_64.rpm matches ./1111/test.txt:root:x:0:0:root:/root:/bin/bash ./1111/test.txt:operator:x:11:0:operator:/root:/sbin/nologin ./1111/test.txt:roott ./passwd:root:x:0:0:root:/root:/bin/bash ./passwd:operator:x:11:0:operator:/root:/sbin/nologin ./passwd:roott [root@oldboyedu ~]# grep -r 'root' ./ ./.bash_history:cd /root/nginx-1.16.0 ./.bash_history:fpm -s dir -t rpm -v 1.16.0 -n nginx -d 'pcre-devel,openssl-devel,zlib-devel' --post-install /root/nginx.sh -f /app/nginx.1.16.0/ ./.bash_history:fpm -s dir -t rpm -v 1.16.0 -n nginx -d 'pcre-devel,openssl-devel,zlib-devel' --post-install /root/nginx.sh -f /app/nginx.1.16.0/ ./.bash_history:mv /root/nginx-1.16.0-1.x86_64.rpm . ./.bash_history:top -d 1 -u root ./.bash_history:pgrep -u root ./.bash_history:pgrep -u -l root ./.bash_history:pgrep -l -u root ./.bash_history:top -d 1 -u root ./.bash_history:wget [root@oldboyedu ~]# kill 9461 ./.bash_history:mkpasswd -l 20 |passwd --stdin root Binary file ./.gem/specs/mirrors.huaweicloud.com%443/repository/rubygems/specs.4.8 matches Binary file ./.gem/specs/mirrors.huaweicloud.com%443/repository/rubygems/latest_specs.4.8 matches Binary file ./.file2.swp matches Binary file ./sysstat-11.7.3-1.x86_64.rpm matches ./1111/test.txt:root:x:0:0:root:/root:/bin/bash ./1111/test.txt:operator:x:11:0:operator:/root:/sbin/nologin ./1111/test.txt:roott ./passwd:root:x:0:0:root:/root:/bin/bash ./passwd:operator:x:11:0:operator:/root:/sbin/nologin ./passwd:roott
[root@oldboyedu ~]# grep -n '.' passwd 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:systemd-network:x:192:192:systemd Network Management:/:/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:abrt:x:173:173::/etc/abrt:/sbin/nologin 19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 20:postfix:x:89:89::/var/spool/postfix:/sbin/nologin 21:nginx:x:998:996:Nginx web server:/var/lib/nginx:/sbin/nologin 22:redis:x:997:995:Redis Database Server:/var/lib/redis:/sbin/nologin 23:ntp:x:38:38::/etc/ntp:/sbin/nologin 24:rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin 25:rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin 26:nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin 27:oldgirl1:x:1010:1010::/home/oldgirl1:/sbin/nologin 28:oldgirl2:x:1011:1011::/home/oldgirl2:/sbin/nologin 29:oldgirl3:x:1012:1012::/home/oldgirl3:/sbin/nologin 30:oldgirl4:x:1013:1013::/home/oldgirl4:/sbin/nologin 31:oldgirl5:x:1014:1014::/home/oldgirl5:/sbin/nologin 32:oldgirl6:x:1015:1015::/home/oldgirl6:/sbin/nologin 33:oldgirl7:x:1016:1016::/home/oldgirl7:/sbin/nologin 34:oldgirl8:x:1017:1017::/home/oldgirl8:/sbin/nologin 35:oldgirl9:x:1018:1018::/home/oldgirl9:/sbin/nologin 36:oldgirl10:x:1019:1019::/home/oldgirl10:/sbin/nologin 37:Root 38:roott [root@oldboyedu ~]# grep -n '.' passwd |grep -w '11' 10:operator:x:11:0:operator:/root:/sbin/nologin 11:games:x:12:100:games:/usr/games:/sbin/nologin [root@oldboyedu ~]# grep -n '.' passwd |grep -w '^11' 11:games:x:12:100:games:/usr/games:/sbin/nologin [root@oldboyedu ~]# grep -n '.' passwd |grep -wA9 '^11' 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:systemd-network:x:192:192:systemd Network Management:/:/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:abrt:x:173:173::/etc/abrt:/sbin/nologin 19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 20:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
[root@oldboyedu ~]# grep -n '.' passwd |grep -w '^20' 20:postfix:x:89:89::/var/spool/postfix:/sbin/nologin [root@oldboyedu ~]# grep -n '.' passwd |grep -wB 9 '^20' 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:systemd-network:x:192:192:systemd Network Management:/:/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:abrt:x:173:173::/etc/abrt:/sbin/nologin 19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 20:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
##### [root@oldboyedu ~]# grep -n '.' passwd |grep -Ew '^15|16' 15:dbus:x:81:81:System message bus:/:/sbin/nologin 16:polkitd:x:999:998:User for polkitd:/:/sbin/nologin [root@oldboyedu ~]# grep -n '.' passwd |grep -Ew '^15|^16' 15:dbus:x:81:81:System message bus:/:/sbin/nologin 16:polkitd:x:999:998:User for polkitd:/:/sbin/nologin [root@oldboyedu ~]# grep -n '.' passwd |grep -EwC4 '^15|^16' 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:systemd-network:x:192:192:systemd Network Management:/:/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:abrt:x:173:173::/etc/abrt:/sbin/nologin 19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 20:postfix:x:89:89::/var/spool/postfix:/sbin/nologin [root@oldboyedu ~]# grep -n '.' passwd |grep -EwC4 '^1[56]' 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:systemd-network:x:192:192:systemd Network Management:/:/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:abrt:x:173:173::/etc/abrt:/sbin/nologin 19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 20:postfix:x:89:89::/var/spool/postfix:/sbin/nologin [root@oldboyedu ~]# grep -n '.' /etc/passwd |grep -EwC4 '^1[56]' 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:systemd-network:x:192:192:systemd Network Management:/:/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:abrt:x:173:173::/etc/abrt:/sbin/nologin 19:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 20:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
正則:html
^node
[root@oldboyedu ~]# grep '^root' passwd root:x:0:0:root:/root:/bin/bash roott [root@oldboyedu ~]# grep '^r' passwd root:x:0:0:root:/root:/bin/bash redis:x:997:995:Redis Database Server:/var/lib/redis:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin roott
$linux
[root@oldboyedu ~]# grep 'h$' passwd root:x:0:0:root:/root:/bin/bash [root@oldboyedu ~]# grep 'bash$' passwd root:x:0:0:root:/root:/bin/bash
[root@oldboyedu ~]# grep -v '^$' passwd #裏面空格和tab鍵 [root@oldboyedu ~]# grep -v '^\s*$' passwd [root@oldboyedu ~]# grep -n '^\s*$' passwd #.點匹配任意一個字符 [root@oldboyedu ~]# grep '.' test.log 123 [root@oldboyedu ~]# grep -o '.' test.log 1 2 3 #環境準備 cat>oldboy.txt<<EOF I am oldboy teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess! my blog is http://oldboy.blog.51cto.com our size is http://blog.oldboyedu.com my qq is 49000448 not 4900000448. my god ,i am not oldbey,but OLDBOY! EOF
轉義,取消特殊字符的特殊含義。nginx
#只過濾以點爲結尾的行 [root@oldboyedu ~]# grep '.$' oldboy.txt I am oldboy teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess! my blog is http://oldboy.blog.51cto.com our size is http://blog.oldboyedu.com my qq is 49000448 not 4900000448. my god ,i am not oldbey,but OLDBOY! [root@oldboyedu ~]# grep '\.$' oldboy.txt I teach linux. not 4900000448. #以m或者n開頭的行
[] 表示多個字符串,會單個匹配[]裏面的內容,[] [^] 的區別web
| 或者redis
[root@oldboyedu ~]# grep '^[mn]' oldboy.txt my blog is http://oldboy.blog.51cto.com my qq is 49000448 not 4900000448. my god ,i am not oldbey,but OLDBOY [root@oldboyedu ~]# grep '^m|^n' oldboy.txt -E my blog is http://oldboy.blog.51cto.com my qq is 49000448 not 4900000448. my god ,i am not oldbey,but OLDBOY! [root@oldboyedu ~]# grep -v '^[mn]' oldboy.txt I am oldboy teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess! our size is http://blog.oldboyedu.com [root@oldboyedu ~]# grep '^[^mn]' oldboy.txt I am oldboy teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess! our size is http://blog.oldboyedu.com [root@oldboyedu ~]# grep '[^mn]' oldboy.txt I am oldboy teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess! my blog is http://oldboy.blog.51cto.com our size is http://blog.oldboyedu.com my qq is 49000448 not 4900000448. my god ,i am not oldbey,but OLDBOY!
*前面一個字符出現0次或0次以上shell
[root@oldboyedu ~]# grep -o '0' oldboy.txt 0 0 0 0 0 0 0 0 [root@oldboyedu ~]# grep -o '0*' oldboy.txt 000 00000
[root@oldboyedu ~]# grep -o '.*' oldboy.txt I am oldboy teacher! I teach linux. I like badminton ball ,billiard ball and chinese chess! my blog is http://oldboy.blog.51cto.com our size is http://blog.oldboyedu.com my qq is 49000448 not 4900000448. my god ,i am not oldbey,but OLDBOY! cat>id.txt<<EOF 湯 610702199107053598 鄒 371481199403259478 莫 52020319810613433X 韓 460106199111137270 榮 530124197504135438 榮 oldboy 榮 babygirl 榮 530124197504135438 阮 360702197902169951 翁 331002198504132813 任 6212231987082X5176 姜 370602198507189574 李A BBBCCC98507189574 趙B BBB602198507189574 霍 320904198403048179 EOF
[root@oldboyedu ~]# grep -E '[0-9]{17}[0-9X]{1}' id.txt 湯 610702199107053598 鄒 371481199403259478 莫 52020319810613433X 韓 460106199111137270 榮 530124197504135438 榮 530124197504135438 阮 360702197902169951 翁 331002198504132813 姜 370602198507189574 霍 320904198403048179 [root@oldboyedu ~]# grep -E -o '[0-9]{17}[0-9X]{1}' id.txt 610702199107053598 371481199403259478 52020319810613433X 460106199111137270 530124197504135438 530124197504135438 360702197902169951 331002198504132813 370602198507189574 320904198403048179
[root@oldboyedu ~]# grep -Ev '^\s*$|#' /etc/nginx/nginx.conf.default >nginx.conf [root@oldboyedu ~]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } #查看配置文件 [root@oldboyedu ~]# grep '^[a-Z]' /etc/ssh/sshd_config HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key SyslogFacility AUTHPRIV AuthorizedKeysFile .ssh/authorized_keys PasswordAuthentication yes ChallengeResponseAuthentication no GSSAPIAuthentication no GSSAPICleanupCredentials no UsePAM yes X11Forwarding yes UseDNS no AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE AcceptEnv XMODIFIERS Subsystem sftp /usr/libexec/openssh/sftp-server [root@oldboyedu ~]# grep -Ev '^$|^#' /etc/ssh/sshd_config
選項: -n #取消默認輸出,p -i #替換 -i.bak #先備份,在替換 -r #支持擴展正則使用 s #替換 g #全局 d #刪除 p #打印 ! #取反 a #追加 i #插入 $ #結尾的行
#取行。 #取單行 [root@oldboyedu ~]# sed -n '3p' passwd bin:x:1:1:bin:/bin:/sbin/nologin #取連續的多行 [root@oldboyedu ~]# sed -n '5,10p' 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 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
#取不連續的多行 [root@oldboyedu ~]# sed -n '5p;10p' passwd sync:x:5:0:sync:/sbin:/bin/sync games:x:12:100:games:/usr/games:/sbin/nologin
[root@oldboyedu ~]# sed '2d' passwd
[root@oldboyedu ~]# sed '2,10d' passwd
[root@oldboyedu ~]# sed '2d;10d' passwd
p' passwd #環境準備 cat >sed.txt<<"EOF" 101,$oldboy,CEO 102,$zhangyao,CTO 103,$Alex,COO 104,$yy,CFO 105,$feixue,CIO EOF #過濾字符串 [root@oldboyedu ~]# sed -n '/yy/p' sed.txt 104,$yy,CFO #過濾多個 [root@oldboyedu ~]# sed -n '/oldboy/p ; /yy/p' sed.txt 101,$oldboy,CEO 104,$yy,CF [root@oldboyedu ~]# sed -nr '/oldboy|yy/p' sed.txt 101,$oldboy,CEO 104,$yy,CFO [root@oldboyedu ~]# sed -n '/oldboy/p;/yy/p' sed.txt 101,$oldboy,CEO 104,$yy,CFO #過濾多個連續的字符串 [root@oldboyedu ~]# sed -n '/oldboy/,/yy/p' sed.txt 101,$oldboy,CEO 102,$zhangyao,CTO 103,$Alex,COO 104,$yy,CFO
a icentos
#在第三行後面追加內容 [root@oldboyedu ~]# sed '3aoldboy' sed.txt 101,$oldboy,CEO 102,$zhangyao,CTO 103,$Alex,COO oldboy 104,$yy,CFO 105,$feixue,CIO #在第四行的上面插入內容 [root@oldboyedu ~]# sed '4ioldboy' sed.txt 101,$oldboy,CEO 102,$zhangyao,CTO 103,$Alex,COO oldboy 104,$yy,CFO 105,$feixue,CIO #插入多行 [root@oldboyedu ~]# sed '4ioldboy\noldgirl' sed.txt 101,$oldboy,CEO 102,$zhangyao,CTO 103,$Alex,COO oldboy oldgirl 104,$yy,CFO 105,$feixue,CIO #追加多行 [root@oldboyedu ~]# sed '3aoldboy\noldgirl' sed.txt 101,$oldboy,CEO 102,$zhangyao,CTO 103,$Alex,COO oldboy oldgirl 104,$yy,CFO 105,$feixue,CIO #在結尾追加內容 [root@oldboyedu ~]# sed '$aoldboy' sed.txt 101,$oldboy,CEO 102,$zhangyao,CTO 103,$Alex,COO 104,$yy,CFO 105,$feixue,CIO oldboy
#全局 [root@oldboyedu ~]# sed 's#oldboy#jinke#g' sed.txt 101,$jinke,CEO 102,$zhangyao,CTO 103,$Alex,COO 104,$yy,CFO 105,$feixue,CIO [root@oldboyedu ~]# sed -i.org 's#oldboy#jinke#g' sed.txt [root@oldboyedu ~]# sed 's#^SELINUX=.*#SELINUX=disabled#g' /etc/sysconfig/selinux #c 原位置替換 [root@oldboyedu ~]# sed '/^SELINUX=/c SELINUX=enforcing' /etc/sysconfig/selinux #第一行到第五行的root替換 [root@oldboyedu ~]# sed '1,5s#root#oldboy#g' passwd #替換第一行的root [root@oldboyedu ~]# sed '1s#root#oldboy#g' passwd #替換特殊字符使用轉義 [root@oldboyedu ~]# sed 's#$#%#g' sed.txt 101,$jinke,CEO% 102,$zhangyao,CTO% 103,$Alex,COO% 104,$yy,CFO% 105,$feixue,CIO% [root@oldboyedu ~]# sed 's#\$#%#g' sed.txt 101,%jinke,CEO 102,%zhangyao,CTO 103,%Alex,COO 104,%yy,CFO 105,%feixue,CIO [root@oldboyedu ~]# sed 's/\/sbin\/nologin/oldboy/g' passwd #後向引用 [root@oldboyedu ~]# ip a s eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:1d:ca:3a brd ff:ff:ff:ff:ff:ff inet 10.0.0.150/24 brd 10.0.0.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 fe80::80b2:dc89:7adf:c7b1/64 scope link noprefixroute valid_lft forever preferred_lft forever [root@oldboyedu ~]# ip a s eth0 | sed -n '3p' inet 10.0.0.150/24 brd 10.0.0.255 scope global noprefixroute eth0 [root@oldboyedu ~]# ip a s eth0 | sed -n '3p' |sed -r 's#.*t (.*)/.*#\1#g' 10.0.0.150 [root@oldboyedu ~]# ip a s eth0 |sed -nr '3s#.*t (.*)/.*#\1#gp' 10.0.0.150 [root@oldboyedu ~]# ifconfig eth0 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.150 netmask 255.255.255.0 broadcast 10.0.0.255 inet6 fe80::80b2:dc89:7adf:c7b1 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:1d:ca:3a txqueuelen 1000 (Ethernet) RX packets 20849 bytes 1902050 (1.8 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 15800 bytes 2443358 (2.3 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@oldboyedu ~]# ifconfig eth0 |sed -n '2p' inet 10.0.0.150 netmask 255.255.255.0 broadcast 10.0.0.255 [root@oldboyedu ~]# ifconfig eth0 |sed -nr '2s#.*t (.*)#\1#gp' 10.0.0.255 [root@oldboyedu ~]# ifconfig eth0 |sed -nr '2s#.*t (.*) n.*#\1#gp' 10.0.0.150 [root@oldboyedu ~]# stat sed.txt File: ‘sed.txt’ Size: 75 Blocks: 8 IO Block: 4096 regular file Device: 803h/2051d Inode: 67160646 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2019-07-25 11:34:50.285127299 +0800 Modify: 2019-07-25 11:34:29.851127028 +0800 Change: 2019-07-25 11:34:29.851127028 +0800 Birth: - cat>/root/hostname_ip.sh<<"EOF" #!/usr/bin/sh source /etc/init.d/functions if [ $# -ne 2 ];then echo "/bin/sh $0 hostname PartIP" exit 1 fi hostnamectl set-hostname $1 if [ $? -eq 0 ];then action "hostname update Successful!" /bin/true else action "hostname update Failed!" /bin/false fi Ip=$(ip a s eth0 |awk -F '[./]' 'NR==3{print $4}') sed -i "s#$Ip#$2#g" /etc/sysconfig/network-scripts/ifcfg-eth[01] if [ $? -eq 0 ];then action "IP update Successful!" /bin/true else action "IP update Failed!" /bin/false fi systemctl restart network bash EOF
選項: -F FS #輸入分隔符,默認是空格 -v #配合輸出分隔符使用 OFS #輸出分隔符 BEGIN END $n #某列 NR #行號 $0 #完整內容 $NF #最後一列 i++ #計算次數 i=i+$N #計算和
[root@oldboy ~]# awk 'NR==3' sed.txt 103,$Alex,COO [root@oldboy ~]# awk 'NR==1,NR==3' sed.txt 101,$jinke,CEO 102,$zhangyao,CTO 103,$Alex,COO [root@oldboy ~]# awk 'NR==1;NR==3' sed.txt 101,$jinke,CEO 103,$Alex,COO [root@oldboy ~]# awk 'NR>1 && NR<3' sed.txt 102,$zhangyao,CTO [root@oldboy ~]# awk 'NR>=1 && NR<=3' sed.txt 101,$jinke,CEO 102,$zhangyao,CTO 103,$Alex,COO
[root@oldboy ~]# awk '/jinke/' sed.txt 101,$jinke,CEO [root@oldboy ~]# awk '!/jinke/' sed.txt 102,$zhangyao,CTO 103,$Alex,COO 104,$yy,CFO 105,$feixue,CIO [root@oldboy ~]# awk '/jinke|yy/' sed.txt 101,$jinke,CEO 104,$yy,CFO [root@oldboy ~]# awk '/jinke/;/yy/' sed.txt 101,$jinke,CEO 104,$yy,CFO [root@oldboy ~]# awk '/jinke/,/yy/' sed.txt 101,$jinke,CEO 102,$zhangyao,CTO 103,$Alex,COO 104,$yy,CFO
[root@oldboy ~]# awk -F '[,]' '{print $3}' sed.txt CEO CTO COO CFO CIO #環境準備 cat >>reg.txt<<EOF Zhang Dandan 41117397 :250:100:175 Zhang Xiaoyu 390320151 :155:90:201 Meng Feixue 80042789 :250:60:50 Wu Waiwai 70271111 :250:80:75 Liu Bingbing 41117483 :250:100:175 Wang Xiaoai 3515064655 :50:95:135 Zi Gege 1986787350 :250:168:200 Li Youjiu 918391635 :175:75:300 Lao Nanhai 918391635 :250:100:175 EOF #根據列進行匹配 #第三列以3開頭的行 [root@oldboy ~]# awk '$3~/^3/' reg.txt Zhang Xiaoyu 390320151 :155:90:201 Wang Xiaoai 3515064655 :50:95:135
[root@oldboy ~]# awk '$3!~/^3/' reg.txt Zhang Dandan 41117397 :250:100:175 Meng Feixue 80042789 :250:60:50 Wu Waiwai 70271111 :250:80:75 Liu Bingbing 41117483 :250:100:175 Zi Gege 1986787350 :250:168:200 Li Youjiu 918391635 :175:75:300 Lao Nanhai 918391635 :250:100:175 #第三列以1或5爲結尾的行 [root@oldboy ~]# awk '$3~/[15]$/' reg.txt Zhang Xiaoyu 390320151 :155:90:201 Wu Waiwai 70271111 :250:80:75 Wang Xiaoai 3515064655 :50:95:135 Li Youjiu 918391635 :175:75:300 Lao Nanhai 918391635 :250:100:175 #顯示姓zhang的人第二次的捐款金額 [root@oldboy ~]# awk -F '[ :]' '/^Zhang/{print $1,$2,$6}' reg.txt Zhang Dandan 100 Zhang Xiaoyu 90 顯示Xiaoyu的捐款.每一個值時都有以$開頭.如$520$200$135 [root@oldboy ~]# awk '/Xiaoyu/{print $4}' reg.txt |tr ':' '$' $155$90$201 [root@oldboy ~]# awk '/Xiaoyu/{print $4}' reg.txt |sed 's#:#$#g' $155$90$201 [root@oldboy ~]# awk '/Xiaoyu/{print $4}' reg.txt |sed 's#:#\$#g' $155$90$201 [root@oldboy ~]# awk -F '[ :]' '/Xiaoyu/{print "$"$5"$"$6"$"$7}' reg.txt $155$90$201 [root@oldboy ~]# awk -F '[ :]' -vOFS='$' '/Xiaoyu/{print "$"$5,$6,$7}' reg.txt $155$90$201 顯示全部人的全名,以姓,名的格式顯示,如Meng,Feixue [root@oldboy ~]# awk '{print $1","$2}' reg.txt Zhang,Dandan Zhang,Xiaoyu Meng,Feixue Wu,Waiwai Liu,Bingbing Wang,Xiaoai Zi,Gege Li,Youjiu Lao,Nanhai [root@oldboy ~]# awk -vOFS=',' '{print $1,$2}' reg.txt Zhang,Dandan Zhang,Xiaoyu Meng,Feixue Wu,Waiwai Liu,Bingbing Wang,Xiaoai Zi,Gege Li,Youjiu Lao,Nanhai [root@oldboy ~]# awk '{print $1,$2}' reg.txt |tr ' ' ',' Zhang,Dandan Zhang,Xiaoyu Meng,Feixue Wu,Waiwai Liu,Bingbing Wang,Xiaoai Zi,Gege Li,Youjiu Lao,Nanhai
[root@oldboy ~]# ifconfig eth0 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.0.0.150 netmask 255.255.255.0 broadcast 10.0.0.255 inet6 fe80::80b2:dc89:7adf:c7b1 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:1d:ca:3a txqueuelen 1000 (Ethernet) RX packets 25397 bytes 2315305 (2.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 19057 bytes 2801352 (2.6 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@oldboy ~]# ifconfig eth0 |awk 'NR==2{print $2}' 10.0.0.150 [root@oldboy ~]# ip a s eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:1d:ca:3a brd ff:ff:ff:ff:ff:ff inet 10.0.0.150/24 brd 10.0.0.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 fe80::80b2:dc89:7adf:c7b1/64 scope link noprefixroute valid_lft forever preferred_lft forever [root@oldboy ~]# ip a s eth0 |awk -F '[ /]*' 'NR==3{print $3}' 10.0.0.150 #排除空行 [root@oldboy ~]# awk '!/^[ \t]*$/' file1 lhlgsd fdssgdgs gsdgd [root@oldboy ~]# awk '!/^\s*$/' file1 lhlgsd fdssgdgs gsdgd [root@oldboy ~]# awk '/^r/' passwd root:x:0:0:root:/root:/bin/bash redis:x:997:995:Redis Database Server:/var/lib/redis:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin roott [root@oldboy ~]# awk '/h$/' passwd root:x:0:0:root:/root:/bin/bash
[root@oldboy ~]# cat test.log 10 20 [root@oldboy ~]# awk '{print $2/$1}' test.log 2 [root@oldboy ~]# awk '{print $2+$1}' test.log 30 [root@oldboy ~]# awk '{print $2-$1}' test.log 10 [root@oldboy ~]# awk '{print $2*$1}' test.log 200 [root@oldboy ~]# awk '{print $2^$1}' test.log 10240000000000 [root@oldboy ~]# awk 'BEGIN{print 10-10 }' 0 [root@oldboy ~]# awk 'BEGIN{print 10+10 }' 20 [root@oldboy ~]# awk 'BEGIN{print 10/10 }' 1 [root@oldboy ~]# awk 'BEGIN{print 10*10 }' 100 [root@oldboy ~]# awk 'BEGIN{print 10^10 }' 10000000000 #計算內存可用的百分比 [root@oldboy ~]# free -m total used free shared buff/cache available Mem: 1980 148 1526 9 305 1646 Swap: 2047 0 2047 [root@oldboy ~]# free -m |awk '/^Mem/' Mem: 1980 148 1526 9 305 1646 [root@oldboy ~]# free -m |awk '/^Mem/{print $4/$2}' 0.770707 [root@oldboy ~]# free -m |awk '/^Mem/{print $4/$2*100}' 77.0707 [root@oldboy ~]# free -m |awk '/^Mem/{print $4/$2*100"%"}' 77.0707%
Awk 數組數組
#統計/etc/services 中空行的數量 [root@oldboy ~]# awk '/^$/' /etc/services |wc -l 17 [root@oldboy ~]# sed -n '/^$/p' /etc/services |wc -l 17 [root@oldboy ~]# grep -c '^$' /etc/services 17 [root@oldboy ~]# awk '/^$/{i++}END{print i}' /etc/services 17 #統計passwd文件中全部shell的數量 [root@oldboy ~]# awk -F: '{print $NF}' passwd |sort |uniq -c 1 /bin/bash 1 /bin/sync 1 /sbin/halt 32 /sbin/nologin 1 /sbin/shutdown [root@oldboy ~]# awk -F: '{shell[$7]++}END{for (i in shell)print i,shell[i]}' passwd |sort -rnk2 |column -t /sbin/nologin 32 /sbin/shutdown 1 /sbin/halt 1 /bin/sync 1 /bin/bash 1 #計算 [root@oldboy ~]# seq 200 |awk '{i=i+$1}END{print i}' 20100 [root@oldboy ~]# seq 100 |awk '{i=i+$1}END{print i}' 5050 [root@oldboy ~]# seq 101 |awk '{i=i+$1}END{print i}' 5151