{"id":543,"date":"2019-04-27T22:15:30","date_gmt":"2019-04-27T14:15:30","guid":{"rendered":"https:\/\/www.linuxcool.com\/?p=543"},"modified":"2022-05-06T17:28:55","modified_gmt":"2022-05-06T09:28:55","slug":"grep","status":"publish","type":"post","link":"https:\/\/www.linuxcool.com\/grep","title":{"rendered":"grep\u547d\u4ee4 – \u5f3a\u5927\u7684\u6587\u672c\u641c\u7d22\u5de5\u5177"},"content":{"rendered":"\n

grep\u6765\u81ea\u4e8e\u82f1\u6587\u8bcd\u7ec4\u201cglobal search regular expression and print out the line\u201d\u7684\u7f29\u5199\uff0c\u610f\u601d\u662f\u7528\u4e8e\u5168\u9762\u641c\u7d22\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5e76\u5c06\u7ed3\u679c\u8f93\u51fa\u3002\u4eba\u4eec\u901a\u5e38\u4f1a\u5c06grep\u547d\u4ee4\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f\u642d\u914d\u4f7f\u7528\uff0c\u53c2\u6570\u4f5c\u4e3a\u641c\u7d22\u8fc7\u7a0b\u4e2d\u7684\u8865\u5145\u6216\u5bf9\u8f93\u51fa\u7ed3\u679c\u7684\u7b5b\u9009\uff0c\u547d\u4ee4\u6a21\u5f0f\u5341\u5206\u7075\u6d3b\u3002<\/p>\n\n\n\n

\u4e0e\u4e4b\u5bb9\u6613\u6df7\u6dc6\u7684\u662fegrep\u547d\u4ee4\u548cfgrep\u547d\u4ee4\u3002\u5982\u679c\u628agrep\u547d\u4ee4\u5f53\u4f5c\u662f\u6807\u51c6\u641c\u7d22\u547d\u4ee4\uff0c\u90a3\u4e48egrep\u5219\u662f\u6269\u5c55\u641c\u7d22\u547d\u4ee4\uff0c\u7b49\u4ef7\u4e8e\u201cgrep -E\u201d\u547d\u4ee4\uff0c\u652f\u6301\u6269\u5c55\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u800cfgrep\u5219\u662f\u5feb\u901f\u641c\u7d22\u547d\u4ee4\uff0c\u7b49\u4ef7\u4e8e\u201cgrep -F\u201d\u547d\u4ee4\uff0c\u4e0d\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u76f4\u63a5\u6309\u7167\u5b57\u7b26\u4e32\u5185\u5bb9\u8fdb\u884c\u5339\u914d\u3002<\/p>\n\n\n\n

\u8bed\u6cd5\u683c\u5f0f\uff1a<\/strong> grep [\u53c2\u6570] \u6587\u4ef6<\/p>\n\n\n\n

\u5e38\u7528\u53c2\u6570\uff1a<\/strong><\/p>\n\n\n\n

-i<\/td>\u5ffd\u7565\u5927\u5c0f\u5199<\/td><\/tr>
-c<\/td>\u53ea\u8f93\u51fa\u5339\u914d\u884c\u7684\u6570\u91cf<\/td><\/tr>
-l<\/td>\u53ea\u5217\u51fa\u7b26\u5408\u5339\u914d\u7684\u6587\u4ef6\u540d\uff0c\u4e0d\u5217\u51fa\u5177\u4f53\u7684\u5339\u914d\u884c<\/td><\/tr>
-n<\/td>\u5217\u51fa\u6240\u6709\u7684\u5339\u914d\u884c\uff0c\u663e\u793a\u884c\u53f7<\/td><\/tr>
-h<\/td>\u67e5\u8be2\u591a\u6587\u4ef6\u65f6\u4e0d\u663e\u793a\u6587\u4ef6\u540d<\/td><\/tr>
-s<\/td>\u4e0d\u663e\u793a\u4e0d\u5b58\u5728\u3001\u6ca1\u6709\u5339\u914d\u6587\u672c\u7684\u9519\u8bef\u4fe1\u606f<\/td><\/tr>
-v<\/td>\u663e\u793a\u4e0d\u5305\u542b\u5339\u914d\u6587\u672c\u7684\u6240\u6709\u884c<\/td><\/tr>
-w<\/td>\u5339\u914d\u6574\u8bcd<\/td><\/tr>
-x<\/td>\u5339\u914d\u6574\u884c<\/td><\/tr>
-r<\/td>\u9012\u5f52\u641c\u7d22<\/td><\/tr>
-q<\/td>\u7981\u6b62\u8f93\u51fa\u4efb\u4f55\u7ed3\u679c\uff0c\u5df2\u9000\u51fa\u72b6\u6001\u8868\u793a\u641c\u7d22\u662f\u5426\u6210\u529f<\/td><\/tr>
-b<\/td>\u6253\u5370\u5339\u914d\u884c\u8ddd\u6587\u4ef6\u5934\u90e8\u7684\u504f\u79fb\u91cf\uff0c\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d<\/td><\/tr>
-o<\/td>\u4e0e-b\u7ed3\u5408\u4f7f\u7528\uff0c\u6253\u5370\u5339\u914d\u7684\u8bcd\u636e\u6587\u4ef6\u5934\u90e8\u7684\u504f\u79fb\u91cf\uff0c\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d<\/td><\/tr>
-F<\/td>\u5339\u914d\u56fa\u5b9a\u5b57\u7b26\u4e32\u7684\u5185\u5bb9<\/td><\/tr>
-E<\/td>\u652f\u6301\u6269\u5c55\u7684\u6b63\u5219\u8868\u8fbe\u5f0f<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n

\u53c2\u8003\u5b9e\u4f8b<\/strong><\/p>\n\n\n\n

\u641c\u7d22\u67d0\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5305\u542b\u67d0\u4e2a\u5173\u952e\u8bcd\u7684\u5185\u5bb9\uff1a<\/p>\n\n\n\n

[root@linuxcool ~]# grep root \/etc\/passwd<\/strong>\nroot:x:0:0:root:\/root:\/bin\/bash\noperator:x:11:0:operator:\/root:\/sbin\/nologin<\/pre>\n\n\n\n

\u641c\u7d22\u67d0\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4ee5\u67d0\u4e2a\u5173\u952e\u8bcd\u5f00\u5934\u7684\u5185\u5bb9\uff1a<\/p>\n\n\n\n

[root@linuxcool ~]# grep ^root \/etc\/passwd<\/strong>\nroot:x:0:0:root:\/root:\/bin\/bash<\/pre>\n\n\n\n

\u641c\u7d22\u591a\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5305\u542b\u67d0\u4e2a\u5173\u952e\u8bcd\u7684\u5185\u5bb9\uff1a<\/p>\n\n\n\n

[root@linuxcool ~]# grep linuxprobe \/etc\/passwd \/etc\/shadow<\/strong>\n\/etc\/passwd:linuxprobe:x:1000:1000:linuxprobe:\/home\/linuxprobe:\/bin\/bash\n\/etc\/shadow:linuxprobe:$6$9Av\/41hCM17T2PrT$hoggWJ3J\/j6IqEOSp62elhdOYPLhQ1qDho7hANcm5fQkPCQdib8KCWGdvxbRvDmqyOarKpWGxd8NAmp3j2Ln00::0:99999:7:::<\/pre>\n\n\n\n

\u641c\u7d22\u591a\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5305\u542b\u67d0\u4e2a\u5173\u952e\u8bcd\u7684\u5185\u5bb9\uff0c\u4e0d\u663e\u793a\u6587\u4ef6\u540d\u79f0\uff1a<\/p>\n\n\n\n

[root@linuxcool ~]# grep -h linuxprobe \/etc\/passwd \/etc\/shadow<\/strong>\nlinuxprobe:x:1000:1000:linuxprobe:\/home\/linuxprobe:\/bin\/bash\nlinuxprobe:$6$9Av\/41hCM17T2PrT$hoggWJ3J\/j6IqEOSp62elhdOYPLhQ1qDho7hANcm5fQkPCQdib8KCWGdvxbRvDmqyOarKpWGxd8NAmp3j2Ln00::0:99999:7:::\n<\/pre>\n\n\n\n

\u8f93\u51fa\u5728\u67d0\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5305\u542b\u67d0\u4e2a\u5173\u952e\u8bcd\u884c\u7684\u6570\u91cf\uff1a<\/p>\n\n\n\n

[root@linuxcool ~]# grep -c root \/etc\/pass<\/strong>wd \/etc\/shadow\n\/etc\/passwd:2\n\/etc\/shadow:1\n<\/pre>\n\n\n\n

\u641c\u7d22\u67d0\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5305\u542b\u67d0\u4e2a\u5173\u952e\u8bcd\u4f4d\u7f6e\u7684\u884c\u53f7\u53ca\u5185\u5bb9\uff1a<\/p>\n\n\n\n

[root@linuxcool ~]# grep -n network anaconda-ks.cfg <\/strong>\n17:network  --bootproto=static --device=ens160 --ip=192.168.10.10 --netmask=255.255.255.0 --onboot=off --ipv6=auto --activate\n18:network  --hostname=linuxcool.com<\/pre>\n\n\n\n

\u641c\u7d22\u67d0\u4e2a\u6587\u4ef6\u4e2d\uff0c\u4e0d\u5305\u542b\u67d0\u4e2a\u5173\u952e\u8bcd\u7684\u5185\u5bb9\uff1a<\/p>\n\n\n\n

[root@linuxcool ~]# grep -v nologin \/etc\/passwd<\/strong>\nroot:x:0:0:root:\/root:\/bin\/bash\nsync:x:5:0:sync:\/sbin:\/bin\/sync\nshutdown:x:6:0:shutdown:\/sbin:\/sbin\/shutdown\nhalt:x:7:0:halt:\/sbin:\/sbin\/halt\nlinuxprobe:x:1000:1000:linuxprobe:\/home\/linuxprobe:\/bin\/bash<\/pre>\n\n\n\n

\u641c\u7d22\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e2d\uff0c\u5305\u542b\u67d0\u4e2a\u5173\u952e\u8bcd\u5185\u5bb9\u7684\u6587\u4ef6\uff0c\u672a\u627e\u5230\u5219\u63d0\u793a\uff1a<\/p>\n\n\n\n

[root@linuxcool ~]# grep -l root *<\/strong>\nanaconda-ks.cfg\ngrep: Desktop: Is a directory\ngrep: Documents: Is a directory\ngrep: Downloads: Is a directory\ninitial-setup-ks.cfg\ngrep: Music: Is a directory\ngrep: Pictures: Is a directory\ngrep: Public: Is a directory\ngrep: Templates: Is a directory\ngrep: Videos: Is a directory\n<\/pre>\n\n\n\n

\u641c\u7d22\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e2d\uff0c\u5305\u542b\u67d0\u4e2a\u5173\u952e\u8bcd\u5185\u5bb9\u7684\u6587\u4ef6\uff0c\u672a\u627e\u5230\u4e0d\u63d0\u793a\uff1a<\/p>\n\n\n\n

[root@linuxcool ~]# grep -sl root *<\/strong>\nanaconda-ks.cfg\ninitial-setup-ks.cfg\n<\/pre>\n\n\n\n

\u9012\u5f52\u641c\u7d22\uff0c\u4e0d\u4ec5\u641c\u7d22\u6307\u5b9a\u76ee\u5f55\uff0c\u8fd8\u641c\u7d22\u5176\u5185\u5b50\u76ee\u5f55\u5185\u662f\u5426\u6709\u5173\u952e\u8bcd\u6587\u4ef6\uff1a<\/p>\n\n\n\n

[root@linuxcool ~]# grep -srl root \/etc<\/strong>\n\/etc\/fstab\n\/etc\/X11\/xinit\/Xclients\n\/etc\/X11\/xinit\/xinitrc\n\/etc\/libreport\/events.d\/collect_dnf.conf\n\/etc\/libreport\/events.d\/bugzilla_anaconda_event.conf\n\/etc\/libreport\/forbidden_words.conf\n\u2026\u2026\u2026\u2026\u2026\u2026\u7701\u7565\u90e8\u5206\u8f93\u51fa\u4fe1\u606f\u2026\u2026\u2026\u2026\u2026\u2026\n<\/pre>\n\n\n\n

\u641c\u7d22\u67d0\u4e2a\u6587\u4ef6\u4e2d\uff0c\u7cbe\u51c6\u5339\u914d\u5230\u67d0\u4e2a\u5173\u952e\u8bcd\u7684\u5185\u5bb9\uff08\u641c\u7d22\u8bcd\u5e94\u4e0e\u6574\u884c\u5185\u5bb9\u5b8c\u5168\u4e00\u6837\u624d\u4f1a\u663e\u793a\uff0c\u6709\u522b\u4e8e\u4e00\u822c\u641c\u7d22\uff09\uff1a<\/p>\n\n\n\n

[root@linuxcool ~]# grep -x cd anaconda-ks.cfg <\/strong>\n[root@linuxcool ~]# grep -x cdrom anaconda-ks.cfg <\/strong>\ncdrom\n<\/pre>\n\n\n\n

\u5224\u65ad\u67d0\u4e2a\u6587\u4ef6\u4e2d\uff0c\u662f\u5426\u5305\u542b\u67d0\u4e2a\u5173\u952e\u8bcd\uff0c\u901a\u8fc7\u8fd4\u56de\u72b6\u6001\u503c\u8f93\u51fa\u7ed3\u679c\uff080\u4e3a\u5305\u542b\uff0c1\u4e3a\u4e0d\u5305\u542b\uff09\uff0c\u65b9\u4fbf\u5728Shell\u811a\u672c\u4e2d\u5224\u65ad\u548c\u8c03\u7528\uff1a<\/p>\n\n\n\n

[root@linuxcool ~]# grep -q linuxprobe anaconda-ks.cfg <\/strong>\n[root@linuxcool ~]# echo $?<\/strong>\n0\n[root@linuxcool ~]# grep -q linuxcool anaconda-ks.cfg <\/strong>\n[root@linuxcool ~]# echo $?<\/strong>\n1<\/pre>\n\n\n\n

\u641c\u7d22\u67d0\u4e2a\u6587\u4ef6\u4e2d\uff0c\u7a7a\u884c\u7684\u6570\u91cf\uff1a<\/p>\n\n\n\n

[root@linuxcool ~]# grep -c ^$ anaconda-ks.cfg <\/strong>\n6<\/pre>\n","protected":false},"excerpt":{"rendered":"

grep\u6765\u81ea\u4e8e\u82f1\u6587\u8bcd\u7ec4\u201cglobal search regular expression and print […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/posts\/543"}],"collection":[{"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/comments?post=543"}],"version-history":[{"count":16,"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/posts\/543\/revisions"}],"predecessor-version":[{"id":9326,"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/posts\/543\/revisions\/9326"}],"wp:attachment":[{"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/media?parent=543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/categories?post=543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxcool.com\/wp-json\/wp\/v2\/tags?post=543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}