{"id":8796,"date":"2023-06-12T16:57:23","date_gmt":"2023-06-12T08:57:23","guid":{"rendered":"\/?p=8796"},"modified":"2023-06-12T17:06:11","modified_gmt":"2023-06-12T09:06:11","slug":"09-grep-egrep-fgrep%e5%91%bd%e4%bb%a4%e4%b8%8e%e5%9f%ba%e6%9c%ac%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"\/?p=8796","title":{"rendered":"09-grep\/egrep\/fgrep\u547d\u4ee4\u4e0e\u57fa\u672c\u6b63\u5219\u8868\u8fbe\u5f0f"},"content":{"rendered":"<h2>\u56de\u987e\uff1a<\/h2>\n<p>bash\u7684\u7279\u6027\uff1ahash,\u53d8\u91cf<\/p>\n<ul>\n<li>\n<p>\u547d\u4ee4hash\uff1a hash\u547d\u4ee4<\/p>\n<\/li>\n<li>\n<p>\u53d8\u91cf\uff1a<\/p>\n<ul>\n<li>\u672c\u5730\u53d8\u91cf\uff08\u4f5c\u7528\u57df\u4e3a\u5f53\u524dshell\u8fdb\u7a0b\uff09<\/li>\n<li>\u73af\u5883\u53d8\u91cf\uff08\u5f53\u524dshell\u8fdb\u7a0b\u53ca\u5176\u5b50\u8fdb\u7a0b\uff09<\/li>\n<li>\u5c40\u90e8\u53d8\u91cf<\/li>\n<li>\u4f4d\u7f6e\u53c2\u6570\u53d8\u91cf<\/li>\n<li>\u7279\u6b8a\u53d8\u91cf<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u53d8\u91cf\u8d4b\u503c\uff1a <code>name=value<\/code>, <code>export name=value<\/code>, <code>delcare -x name=value<\/code><\/p>\n<\/li>\n<li>\n<p>\u53d8\u91cf\u5f15\u7528\uff1a<code>$name<\/code>, <code>${name}<\/code><\/p>\n<p>\u6709\u65f6\u5019\uff0c\u53d8\u91cf\u5f15\u7528\u7684\u82b1\u62ec\u53f7{}\u4e0d\u53ef\u4ee5\u7701\u7565\uff1a<\/p>\n<pre><code class=\"language-shell\">[lemon@VM-4-10-centos ~]$ animal=sheep\n[lemon@VM-4-10-centos ~]$ echo ${animal}\nsheep\n[lemon@VM-4-10-centos ~]$ echo $animal\nsheep\n[lemon@VM-4-10-centos ~]$ echo \"There are some $animal\"\nThere are some sheep\n[lemon@VM-4-10-centos ~]$ echo \"There are some $animals\"\nThere are some\n[lemon@VM-4-10-centos ~]$ echo \"There are some $animal s\"\nThere are some sheep s\n[lemon@VM-4-10-centos ~]$ echo \"There are some ${animal}s\"\nThere are some sheeps<\/code><\/pre>\n<\/li>\n<li>\n<p>\u64a4\u9500\u53d8\u91cf\uff1aunset name<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230609143828295.png\" alt=\"image-20230609143828295\" \/> <\/p>\n<\/li>\n<li>\n<p>bash\u811a\u672c\u7f16\u7a0b\uff0c\u8fd0\u884c\u811a\u672c<\/p>\n<pre><code class=\"language-shell\">#!\/bin\/bash\n#<\/code><\/pre>\n<\/li>\n<li>\n<p>bash\u7684\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<ul>\n<li>profile\u7c7b\uff1a\u767b\u5f55\u5f0fshell (su -l)<\/li>\n<li>\u767b\u5f55\u5f0fshell: <code>\/etc\/profile --&gt; \/etc\/profiled.d\/* --&gt; ~\/.bashrc --&gt; \/etc\/bashrc<\/code><\/li>\n<li>bashrc\u7c7b\uff1a\u975e\u767b\u5f55\u5f0fshell (su\uff0c\u56fe\u50cf\u9875\u9762\u6253\u5f00\u7684\u547d\u4ee4\u884c\u7a97\u53e3\uff0cshell\u811a\u672c\u8fd0\u884c\u65f6\u7684shell)<\/li>\n<li>\u975e\u767b\u5f55\u5f0fshell: <code>~\/.bashrc --&gt;\/etc\/bashrc --&gt; \/etc\/profile.d\/*.sh<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\u6587\u672c\u5904\u7406\u5de5\u5177<\/h2>\n<p>Linux\u4e0a\u6587\u672c\u5904\u7406\u4e09\u5251\u5ba2\uff1a<\/p>\n<ul>\n<li><code>grep<\/code>,<code>egrep<\/code>,<code>fgrep<\/code>\uff1a\u6587\u672c\u8fc7\u6ee4\u5de5\u5177\uff1b\uff08\u6a21\u5f0f\uff1apattern\uff09\n<ul>\n<li><code>grep<\/code>: \u57fa\u672c\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c-E, -F<\/li>\n<li><code>egrep<\/code>:\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c -G, -F<\/li>\n<li><code>fgrep<\/code>: \u4e0d\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c -F<\/li>\n<\/ul>\n<\/li>\n<li><code>sed<\/code>\uff1a\u6587\u672c\u7f16\u8f91\u5de5\u5177\uff1bstrem editor\uff0c\u6d41\u7f16\u8f91\u5668\uff1b<\/li>\n<li><code>awk<\/code>\uff1a\u6587\u672c\u62a5\u544a\u751f\u6210\u5668\uff1b\uff08\u683c\u5f0f\u5316\u6587\u672c\uff09\uff1bLinux\u4e0a\u7684\u5b9e\u73b0\u4e3agawk\uff1b<\/li>\n<\/ul>\n<p>\u90fd\u4f1a\u7528\u5230\u6b63\u5219\u8868\u8fbe\u5f0f(Regual Expression,\u7b80\u5199REGEXP):<\/p>\n<p><strong><em>\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a\u7531\u4e00\u7c7b\u7279\u6b8a\u5b57\u7b26\u53ca\u6587\u672c\u5b57\u7b26\u6240\u7f16\u5199\u7684\u6a21\u5f0f\uff0c\u5176\u4e2d\u6709\u4e9b\u5b57\u7b26\u4e0d\u8868\u793a\u5176\u5b57\u9762\u610f\u4e49\uff0c\u800c\u662f\u7528\u4e8e\u8868\u793a\u63a7\u5236\u6216\u901a\u914d\u7684\u529f\u80fd<\/em><\/strong><\/p>\n<p>\u5206\u4e24\u7c7b\uff1a<\/p>\n<ul>\n<li>\u57fa\u672c\u6b63\u5219\u8868\u8fbe\u5f0f\uff1aBRE<\/li>\n<li>\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f\uff1aERE<\/li>\n<\/ul>\n<h2>grep<\/h2>\n<p><strong>grep:Global search REgual expression and Print out the line<\/strong><\/p>\n<p><strong>\u4f5c\u7528<\/strong>\uff1a\u6587\u672c\u641c\u7d22\u5de5\u5177\uff0c\u6839\u636e\u7528\u6237\u6307\u5b9a\u7684\u201c\u6a21\u5f0f\uff08\u8fc7\u6ee4\u6761\u4ef6\uff09\u201d\u5bf9\u76ee\u6807\u6587\u672c\u9010\u884c\u8fdb\u884c\u5339\u914d\u68c0\u67e5\uff1b\u6253\u5370\u5339\u914d\u5230\u7684\u884c;<\/p>\n<p><strong>\u6a21\u5f0f<\/strong>\uff1a\u7531\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5143\u5b57\u7b26\u53ca\u6587\u672c\u5b57\u7b26\u6240\u7f16\u5199\u51fa\u7684\u8fc7\u6ee4\u6761\u4ef6;<\/p>\n<pre><code class=\"language-shell\">grep [OPTIONS] PATTERN [FILE...]\ngrep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230609150149949.png\" alt=\"image-20230609150149949\" \/> <\/p>\n<h3>\u9009\u9879\u64cd\u4f5c<\/h3>\n<p><strong>OPT\u03a0ONS:<\/strong><\/p>\n<ul>\n<li>\n<p><code>--color=auto<\/code>: \u5bf9\u5339\u914d\u5230\u7684\u6587\u672c\u7740\u8272\u540e\u9ad8\u4eae\u663e\u793a\uff1b\u5f53\u7136\uff0c\u8fd9\u4e2a\u662f\u9ed8\u8ba4\u7684\uff0c\u56e0\u4e3a\u8d85\u94fe\u63a5\u522b\u540d<code>alias<\/code><\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230609162222801.png\" alt=\"image-20230609162222801\" \/> <\/p>\n<\/li>\n<li>\n<p><code>-i<\/code>:  ignorecase,\u5ffd\u7565\u5b57\u7b26\u7684\u5927\u5c0f\u5199\uff1b<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230609162339834.png\" alt=\"image-20230609162339834\" \/> <\/p>\n<\/li>\n<li>\n<p><code>-o<\/code>: \u4ec5\u663e\u793a\u5339\u914d\u5230\u7684\u5b57\u7b26\u4e32\u672c\u8eab;<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230609162422968.png\" alt=\"image-20230609162422968\" \/> <\/p>\n<\/li>\n<li>\n<p>-<code>v<\/code>: --Invert-match:\u663e\u793a\u4e0d\u80fd\u88ab\u6a21\u5f0f\u5339\u914d\u5230\u7684\u884c\uff0c\u5c31\u662f\u547d\u4e2d\u5b57\u7b26\u4ee5\u5916\u7684\uff0c\u53cd\u7740\u6765\u7684;<\/p>\n<\/li>\n<li>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230609162619834.png\" alt=\"image-20230609162619834\" \/> <\/p>\n<\/li>\n<li>\n<p><code>-E<\/code>:\u652f\u6301\u4f7f\u7528\u6269\u5c55\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5143\u5b57\u7b26;<\/p>\n<\/li>\n<li>\n<p><code>-q<\/code>,<code>-quiet<\/code>,<code>-silent<\/code>: \u9759\u9ed8\u6a21\u5f0f\uff0c\u5373\u4e0d\u8f93\u51fa\u4efb\u4f55\u4fe1\u606f;<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230609163954782.png\" alt=\"image-20230609163954782\" \/> <\/p>\n<\/li>\n<li>\n<p><code>-A #<\/code>:after \u663e\u793a\u540e#\u884c<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230609161733524.png\" alt=\" \" \/> <\/p>\n<\/li>\n<li>\n<p><code>-B #<\/code> \uff1abefore, \u663e\u793a\u524d#\u884c<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230609161801591.png\" alt=\"image-20230609161801591\" \/> <\/p>\n<\/li>\n<li>\n<p><code>-C #<\/code>\uff1acontext, \u524d\u540e\u5404#\u884c<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230609162032911.png\" alt=\"image-20230609162032911\" \/> <\/p>\n<\/li>\n<\/ul>\n<h3>\u57fa\u672c\u6b63\u5219\u8868\u8fbe\u5f0f\u5143\u5b57\u7b26<\/h3>\n<h4>\u5b57\u7b26\u5339\u914d<\/h4>\n<ul>\n<li>\n<p><code>.<\/code>:\u5339\u914d\u4efb\u610f\u5355\u4e2a\u5b57\u7b26;<\/p>\n<\/li>\n<li>\n<p><code>[]<\/code>:\u5339\u914d\u6307\u5b9a\u8303\u56f4\u5185\u7684\u4efb\u610f\u5355\u4e2a\u5b57\u7b26;<\/p>\n<\/li>\n<li>\n<p><code>[^]<\/code>:\u5339\u914d\u6307\u5b9a\u8303\u56f4\u5916\u7684\u4efb\u610f\u5355\u4e2a\u5b57\u7b26\uff1a<code>[:digit:][:lower:][:upper:].[:alpha:][:alnum:][:punct:][:space:]<\/code><\/p>\n<pre><code># \u5339\u914d\u5305\u542b\uff1ar\u548ct\u4e4b\u95f4\u4efb\u610f\u4e24\u4e2a\u5b57\u7b26\n[root@rhino010 ~]# grep \"r..t\" \/etc\/passwd\nroot:x:0:0:root:\/root:\/bin\/bash\noperator:x:11:0:operator:\/root:\/sbin\/nologin\nftp:x:14:50:FTP User:\/var\/ftp:\/sbin\/nologin\nftpuser:x:1002:50::\/var\/ftp\/public_root:\/bin\/bash\ndockerroot:x:994:990:Docker User:\/var\/lib\/docker:\/sbin\/nologin\n# \u5339\u914d\u5305\u542b\uff1ar\u548ct\u4e4b\u95f4\u4e24\u4e2a\u5b57\u6bcd\n[root@rhino010 ~]# grep \"r[[:alpha:]][[:alpha:]]t\" \/etc\/passwd\nroot:x:0:0:root:\/root:\/bin\/bash\noperator:x:11:0:operator:\/root:\/sbin\/nologin\nftpuser:x:1002:50::\/var\/ftp\/public_root:\/bin\/bash\ndockerroot:x:994:990:Docker User:\/var\/lib\/docker:\/sbin\/nologin\n# \u5339\u914d\u5305\u542b\uff1ar\u548ct\u4e4b\u95f4\uff0c\u7b2c\u4e00\u4e2a\u975e\u5b57\u6bcd\uff0c\u7b2c\u4e8c\u4e2a\u4e3a\u5b57\u6bcd\uff08\u4e3b\u8981\u628a^\u7b26\u53f7\u653e\u5728\u524d\u9762\uff0c\u8868\u793a\u975e\uff0c\u53d6\u53cd\uff09\n[root@rhino010 ~]# grep \"r[^[:alpha:]][[:alpha:]]t\" \/etc\/passwd\nftp:x:14:50:FTP User:\/var\/ftp:\/sbin\/nologin\nftpuser:x:1002:50::\/var\/ftp\/public_root:\/bin\/bash<\/code><\/pre>\n<\/li>\n<\/ul>\n<h4>\u5339\u914d\u6b21\u6570<\/h4>\n<p>\u7528\u5728\u8981\u6307\u5b9a\u5176\u51fa\u73b0\u7684\u6b21\u6570\u7684\u5b57\u7b26\u7684\u540e\u9762\uff0c\u7528\u4e8e\u9650\u5236\u5176\u524d\u9762\u5b57\u7b26\u51fa\u73b0\u7684\u6b21\u6570\uff1a\u9ed8\u8ba4\u5de5\u4f5c\u4e8e\u8d2a\u5a6a\u6a21\u5f0f\uff1a<\/p>\n<ul>\n<li><code>*<\/code>:\u5339\u914d\u5176\u524d\u9762\u7684\u5b57\u7b26\u4efb\u610f\u6b21\uff1b0,1\uff0c\u591a\u6b21\uff1b<\/li>\n<li><code>.*<\/code>:\u5339\u914d\u4efb\u610f\u957f\u5ea6\u7684\u4efb\u610f\u5b57\u7b26;<\/li>\n<li><code>\\?<\/code>:\u5339\u914d\u5176\u524d\u9762\u7684\u5b57\u7b260\u6b21\u62161\u6b21\uff1a\u5373\u5176\u524d\u9762\u7684\u5b57\u7b26\u662f\u53ef\u6709\u53ef\u65e0\u7684;<\/li>\n<li><code>\\+<\/code>:\u5339\u914d\u5176\u524d\u9762\u7684\u5b57\u7b261\u6b21\u6216\u591a\u6b21\uff1b\u5373\u5176\u9762\u7684\u5b57\u7b26\u8981\u51fa\u73b0\u81f3\u5c111\u6b21\uff1b<\/li>\n<li><code>\\{m\\}<\/code>\uff1a\u5339\u914d\u5176\u524d\u9762\u7684\u5b57\u7b26m\u6b21\uff1b<\/li>\n<li><code>\\{m, n\\}<\/code>\uff1a\u5339\u914d\u5176\u524d\u9762\u7684\u5b57\u7b26 \u81f3\u5c11m\u6b21\uff0c\u81f3\u591an\u6b21<\/li>\n<li><code>\\{0, n\\}<\/code>\uff1a\u5339\u914d\u5176\u524d\u9762\u7684\u5b57\u7b26 \u81f3\u591an\u6b21<\/li>\n<li><code>\\{m, \\}<\/code>\uff1a\u5339\u914d\u5176\u524d\u9762\u7684\u5b57\u7b26 \u81f3\u591an\u6b21<\/li>\n<\/ul>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230609170749111.png\" alt=\"image-20230609170749111\" \/> <\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230609171416136.png\" alt=\"image-20230609171416136\" \/> <\/p>\n<h4>\u4f4d\u7f6e\u951a\u5b9a<\/h4>\n<ul>\n<li>\n<p><code>^<\/code>\u884c\u9996\u951a\u5b9a\uff1b\u7528\u4e8e\u6a21\u5f0f\u7684\u6700\u5de6\u4fa7\uff1b<\/p>\n<p><code>grep &#039;^root&#039; \/etc\/passwd<\/code><\/p>\n<\/li>\n<li>\n<p><code>$<\/code>\u884c\u5c3e\u951a\u5b9a\uff1b\u7528\u4e8e\u6a21\u5f0f\u7684\u6700\u53f3\u4fa7\uff1b<\/p>\n<p><code>grep &#039;^root&#039; \/etc\/passwd<\/code><\/p>\n<\/li>\n<li>\n<p><code>^PATTERN$<\/code>\uff1a\u7528\u4e8ePATTERN\u6765\u5339\u914d\u6574\u884c\uff1b<\/p>\n<ul>\n<li><code>^$<\/code>: \u7a7a\u767d\u884c\uff1b<\/li>\n<li><code>^[[:SPACE:]]*$<\/code>: \u7a7a\u884c\u6216\u5305\u542b\u7a7a\u767d\u5b57\u7b26\u7684\u884c\uff1b<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong><font color=red>\u5355\u8bcd\uff1a\u8fde\u7eed\u7684\u975e\u7279\u6b8a\u5b57\u7b26\u7ec4\u6210\u7684\u5b57\u7b26(\u5b57\u7b26\u4e32)\u90fd\u79f0\u4e3a\u5355\u8bcd<\/font><\/strong><\/p>\n<\/li>\n<li>\n<p><code>\\&lt;<\/code>\u6216<code>\\b<\/code>\uff1a\u8bcd\u9996\u951a\u5b9a, \u7528\u4e8e\u5355\u8bcd\u6a21\u5f0f\u7684\u5de6\u4fa7\uff1b<\/p>\n<\/li>\n<li>\n<p><code>\\&gt;<\/code>\u6216<code>\\b<\/code>: \u8bcd\u5c3e\u951a\u5b9a\uff0c\u7528\u4e8e\u5355\u8bcd\u6a21\u5f0f\u7684\u53f3\u4fa7\uff1b<\/p>\n<\/li>\n<li>\n<p><code>\\&lt;PATTERN\\&gt;<\/code>:\u5339\u914d\u5b8c\u6574\u5355\u8bcd<\/p>\n<\/li>\n<\/ul>\n<h4><font color=green><strong>\u7ec3\u4e60\uff1a<\/strong><\/font><\/h4>\n<ul>\n<li>\n<p>\u663e\u793a\/etc\/passwd\u6587\u4ef6\u4e2d\u4e0d\u4ee5\/bin\/bash\u7ed3\u5c3e\u7684\u884c;<\/p>\n<pre><code class=\"language-bash\">grep  -v '\/bin\/bash$'  \/etc\/passwd<\/code><\/pre>\n<\/li>\n<li>\n<p>\u627e\u51fa\/etcpasswd\u6587\u4ef6\u4e2d\u4e24\u4f4d\u6570\u6216\u4e09\u4f4d\u6570;<\/p>\n<pre><code class=\"language-bash\"># grep '[[:digit:]]\\{2,3\\}'  \/etc\/passwd\ngrep '\\<[0-9]\\{2,3\\}\\>'  \/etc\/passwd<\/code><\/pre>\n<\/li>\n<li>\n<p>\u627e\u51fa\/etc\/rc.d\/rc.sysinit(centos6)\u6216\/etc\/grub2.cfg(centos7)\u6587\u4ef6\u4e2d\uff0c\u4ee5\u81f3\u5c11\u4e00\u4e2a\u7a7a\u767d\u5b57\u7b26\u5f00\u5934\uff0c\u4e14\u540e\u9762\u975e\u7a7a\u767d\u5b57\u7b26\u7684\u884c;<\/p>\n<pre><code class=\"language-bash\">grep \"^[[:space:]]\\+[^[:space:]]\" \/etc\/grub2.cfg<\/code><\/pre>\n<\/li>\n<li>\n<p>\u627e\u51fa&quot;netstat -tan\u201d\u547d\u4ee4\u7684\u7ed3\u679c\u4e2d\u4ee5LISTEN\u540e\u8ddf0\u30011\u4e2a\u6216\u591a\u4e2a\u7a7a\u767d\u5b57\u7b26\u7ed3\u5c3e\u7684\u884c;<\/p>\n<pre><code class=\"language-bash\">netstat -tan | grep \"LISTEN[[:space:]]*$\"<\/code><\/pre>\n<\/li>\n<\/ul>\n<h4>\u5206\u7ec4\u53ca\u5f15\u7528<\/h4>\n<p><code>\\(\\)<\/code>:\u5c06\u4e00\u4e2a\u6216\u591a\u4e2a\u5b57\u7b26\u6346\u7ed1\u5728\u4e00\u8d77\uff0c\u5f53\u505a\u4e00\u4e2a\u6574\u4f53\u5904\u7406<\/p>\n<p><code>\\(xy\\)*ab<\/code><\/p>\n<p><strong>Note<\/strong>:\u5206\u7ec4\u62ec\u53f7\u4e2d\u7684\u6a21\u5f0f\u5339\u914d\u5230\u7684\u5185\u5bb9\u4f1a\u88ab\u6b63\u5219\u8868\u8fbe\u5f0f\u5f15\u64ce\u81ea\u52a8\u8bb0\u5f55\u4e8e\u5185\u90e8\u7684\u53d8\u91cf\u4e2d\uff0c\u8fd9\u4e9b\u53d8\u91cf\u4e3a\uff1a<\/p>\n<ul>\n<li>\\1:\u6a21\u5f0f\u4ece\u5de6\u4fa7\u8d77\uff0c\u7b2c\u4e00\u4e2a\u5de6\u62ec\u53f7\u4ee5\u53ca\u4e0e\u4e4b\u5339\u914d\u7684\u53f3\u62ec\u53f7\u4e4b\u95f4\u7684\u6a21\u5f0f\u6240\u5339\u914d\u5230\u7684\u5b57\u7b26\uff1a<\/li>\n<li>\\2:\u6a21\u5f0f\u4ece\u5de6\u4fa7\u8d77\uff0c\u7b2c\u4e8c\u4e2a\u5de6\u62ec\u53f7\u4ee5\u53ca\u4e0e\u4e4b\u5339\u914d\u7684\u53f3\u62ec\u53f7\u4e4b\u95f4\u7684\u6a21\u5f0f\u6240\u5339\u914d\u5230\u7684\u5b57\u7b26\uff1a<\/li>\n<li>\\3: ...<\/li>\n<\/ul>\n<pre><code>[root@VM-4-10-centos ~]# cat lovers.txt\nHe likes his lover.\nHe loves his lover.\nShe likes her liker.\nShe loves her liker.\n[root@VM-4-10-centos ~]# grep &quot;\\(l..e\\).*\\1&quot; lovers.txt\nHe loves his lover.\nShe likes her liker.\n[root@VM-4-10-centos ~]# grep &quot;\\(l..e\\).*\\2&quot; lovers.txt\ngrep: \u65e0\u6548\u7684\u5411\u540e\u5f15\u7528\n[root@VM-4-10-centos ~]# grep &quot;^\\(r..t\\).*\\1&quot; \/etc\/passwd\nroot:x:0:0:root:\/root:\/bin\/bash<\/code><\/pre>\n<p><strong>\u540e\u5411\u5f15\u7528\uff1a<\/strong>\u5f15\u7528\u524d\u9762\u5206\u7ec4\u62ec\u53f7\u4e2d\u6a21\u5f0f\u6240\u5339\u914d\u5230\u7684\u5b57\u7b26<\/p>\n<h2>egrep<\/h2>\n<h3>\u6982\u8ff0<\/h3>\n<p>extend\uff0c\u652f\u6301\u6269\u5c55\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5b9e\u73b0\u7c7b\u4f3c\u4e8egrep\u6587\u672c\u8fc7\u6ee4\u529f\u80fd\uff1a <code>grep -E<\/code><\/p>\n<p>egrep [OPTIONS] PATTERN [FILE...]<\/p>\n<p>\u9009\u9879\uff1a<\/p>\n<ul>\n<li>\n<p>-i, -o, -v, -q, -A, -B, -C<\/p>\n<\/li>\n<li>\n<p>-G:\u652f\u6301\u57fa\u672c\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<\/li>\n<\/ul>\n<p>\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5143\u5b57\u7b26\uff1a<\/p>\n<ul>\n<li>\u5b57\u7b26\u5339\u914d\uff1a\n<ul>\n<li><code>.<\/code>:\u4efb\u610f\u5355\u4e2a\u5b57\u7b26<\/li>\n<li><code>[]<\/code>:\u6307\u5b9a\u8303\u56f4\u5185\u7684\u4efb\u610f\u5355\u4e2a\u5b57\u7b26<\/li>\n<li><code>[^]<\/code>:\u6307\u5b9a\u8303\u56f4\u5916\u7684\u4efb\u610f\u5355\u4e2a\u5b57\u7b26<\/li>\n<\/ul>\n<\/li>\n<li>\u6b21\u6570\u5339\u914d\uff1a\n<ul>\n<li><code>*<\/code>:\u4efb\u610f\u6b21\uff0c0,1\u6216\u591a\u6b21\uff1a<\/li>\n<li><code>?<\/code>:0\u6b21\u62161\u6b21\uff0c\u5176\u524d\u7684\u5b57\u7b26\u662f\u53ef\u6709\u53ef\u65e0\u7684\uff1a<\/li>\n<li><code>+<\/code>:\u5176\u524d\u5b57\u7b26\u81f3\u5c111\u6b21\uff1b<\/li>\n<li><code>{m}<\/code>:\u5176\u524d\u7684\u5b57\u7b26m\u6b21\uff1b<\/li>\n<li><code>{m,n}<\/code>:\u81f3\u5c11m\u6b21\uff0c\u81f3\u591an\u6b21;<br \/>\n<code>{0,n}<\/code><br \/>\n<code>{m,}<\/code><\/li>\n<\/ul>\n<\/li>\n<li>\u4f4d\u7f6e\u951a\u5b9a\n<ul>\n<li><code>^<\/code>:\u884c\u9996\u951a\u5b9a;<\/li>\n<li><code>$<\/code>:\u884c\u5c3e\u951a\u5b9a;<\/li>\n<li><code>\\&lt;<\/code>,<code>\\b<\/code>:\u8bcd\u9996\u951a\u5b9a;<\/li>\n<li><code>\\&gt;<\/code>,<code>\\b<\/code>:\u8bcd\u5c3e\u951a\u5b9a;<\/li>\n<\/ul>\n<\/li>\n<li>\u5206\u7ec4\u53ca\u5f15\u7528\n<ul>\n<li><code>()<\/code>:\u5206\u7ec4\uff1a\u62ec\u53f7\u5185\u7684\u6a21\u5f0f\u5339\u914d\u5230\u7684\u5b57\u7b26\u4f1a\u88ab\u8bb0\u5f55\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u5f15\u64ce\u7684\u5185\u90e8\u53d8\u91cf\u4e2d;<\/li>\n<li>\u540e\u5411\u5f15\u7528\uff1a<code>\\1<\/code>,<code>\\2<\/code>\uff0c\u2026<br \/>\n\u6216\uff1a<\/li>\n<li><code>a|b<\/code>:a\u6216\u8005b<\/li>\n<li><code>C | cat<\/code>:C\u6216cat<\/li>\n<li><code>(clC)at<\/code>: Cat\u6216cat<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4><font color=green><strong>\u7ec3\u4e60\uff1a<\/strong><\/font><\/h4>\n<ul>\n<li>\n<p>\u663e\u793a\/etc\/passwd\u6587\u4ef6\u4e2d\u4e0d\u4ee5\/bin\/bash\u7ed3\u5c3e\u7684\u884c;<\/p>\n<pre><code class=\"language-bash\">egrep  -v '\/bin\/bash$'  \/etc\/passwd\n# grep -E<\/code><\/pre>\n<\/li>\n<li>\n<p>\u627e\u51fa\/etcpasswd\u6587\u4ef6\u4e2d\u4e24\u4f4d\u6570\u6216\u4e09\u4f4d\u6570;<\/p>\n<pre><code class=\"language-bash\">egrep '\\<[0-9]{2,3}\\>'  \/etc\/passwd\n# grep -E<\/code><\/pre>\n<\/li>\n<li>\n<p>\u627e\u51fa\/etc\/rc.d\/rc.sysinit(centos6)\u6216\/etc\/grub2.cfg(centos7)\u6587\u4ef6\u4e2d\uff0c\u4ee5\u81f3\u5c11\u4e00\u4e2a\u7a7a\u767d\u5b57\u7b26\u5f00\u5934\uff0c\u4e14\u540e\u9762\u975e\u7a7a\u767d\u5b57\u7b26\u7684\u884c;<\/p>\n<pre><code class=\"language-bash\">egrep \"^[[:space:]]+[^[:space:]]\" \/etc\/grub2.cfg\n# grep -E<\/code><\/pre>\n<\/li>\n<li>\n<p>\u627e\u51fa&quot;netstat -tan\u201d\u547d\u4ee4\u7684\u7ed3\u679c\u4e2d\u4ee5LISTEN\u540e\u8ddf0\u30011\u4e2a\u6216\u591a\u4e2a\u7a7a\u767d\u5b57\u7b26\u7ed3\u5c3e\u7684\u884c;<\/p>\n<pre><code class=\"language-bash\">netstat -tan | egrep \"LISTEN[[:space:]]*$\"\n# grep -E<\/code><\/pre>\n<\/li>\n<li>\n<p>\u627e\u51fa\/proc\/meminfo\u6587\u4ef6\u4e2d\uff0c\u6240\u6709\u5728\u5927\u5199\u6216\u5c0f\u5199S\u5f00\u5934\u7684\u884c\uff1a\u81f3\u5c11\u6709\u4e09\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff1b<\/p>\n<pre><code class=\"language-bash\">grep  -i '^S' \/proc\/meminfo\negrep  '^(s|S)' \/proc\/meminfo\ngrep  '^[Ss]' \/proc\/meminfo<\/code><\/pre>\n<\/li>\n<li>\n<p>\u663e\u793a\u5f53\u524d\u7cfb\u7edf\u4e0aroot\u3001centos\u6216user1\u7528\u6237\u7684\u76f8\u5173\u4fe1\u606f<\/p>\n<pre><code class=\"language-bash\">grep -E \"^(root|vents|user1)\\>\"<\/code><\/pre>\n<\/li>\n<li>\n<p>\u627e\u51fa\/etc\/rc.d\/Init.d\/functions\u6587\u4ef6\u4e2d\u67d0\u5355\u8bcd\u540e\u9762\u8ddf\u4e00\u4e2a\u5c0f\u62ec\u53f7\u7684\u884c<\/p>\n<pre><code class=\"language-bash\">egrep '[_[:alnum:]]+\\(\\)' \/etc\/rc.d\/init.d\/functions\negrep '\\<.+\\>+\\(\\)' \/etc\/rc.d\/init.d\/functions<\/code><\/pre>\n<\/li>\n<li>\n<p>\u4f7f\u7528echo\u547d\u4ee4\u8f93\u51fa\u4e00\u7edd\u5bf9\u8def\u5f84\uff0c\u4f7f\u7528egrep\u53d6\u51fa\u57fa\u540d\uff1b<\/p>\n<pre><code class=\"language-bash\"># \u627e\u8bcd\u5c3e\u7684\u975e\u659c\u7ebf\u5b57\u7b26\necho \/home\/user\/documents\/file.txt | egrep -o '[^\/]+\/?$'\necho \/home\/user\/documents\/file.txt | egrep -o '[^\/]+$'\n\n# \u53d6\u8def\u5f84\u540d\uff0c\u7c7b\u4f3c\u4e8e\u5bf9\u5176\u6267\u884cdirname\u547d\u4ee4\u7684\u7ed3\u679c\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u627e\u51faifconfig\u547d\u4ee4\u7ed3\u679c\u4e2d\u76841-255\u4e4b\u95f4\u7684\u6570\u503c<\/p>\n<pre><code class=\"language-bash\">ifconfig | grep -E -o \"\\<[1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]\\>\"<\/code><\/pre>\n<\/li>\n<li>\n<p>\u8bfe\u5916\u4f5c\u4e1a\uff1a\u627e\u51faifconfig\u547d\u4ee4\u7ed3\u679c\u4e2d\u7684IP\u5730\u5740<\/p>\n<pre><code class=\"language-bash\">ifconfig | grep -E -o '[[:digit:]]+\\.[[:digit:]]+\\.[[:digit:]]+\\.[[:digit:]]'<\/code><\/pre>\n<\/li>\n<li>\n<p>\u6dfb\u52a0\u7528\u6237bash,testbash,basher\u4ee5\u53canologin(\u5176shel\u4e3a\/sbln\/nologin):\u800c\u540e\u627e\u51fa\/etc\/passwd\u6587\u4ef6\u4e2d\u7528\u6237\u540d\u540cshell\u540d\u7684\u884c\uff1b<\/p>\n<pre><code class=\"language-shell\">[root@VM-4-10-centos data]# useradd bash\n[root@VM-4-10-centos data]# useradd basher\n[root@VM-4-10-centos data]# useradd -s \/sbin\/nologin nologin\n[root@VM-4-10-centos data]# useradd testbash\n[root@VM-4-10-centos data]# tail \/etc\/passwd\nlighthouse:x:1002:1002::\/home\/lighthouse:\/bin\/bash\nlemon:x:1003:1003::\/home\/lemon:\/bin\/bash\ntss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:\/dev\/null:\/sbin\/nologin\nredis:x:995:990:Redis Database Server:\/var\/lib\/redis:\/sbin\/nologin\nx:x:1004:1004::\/home\/x:\/bin\/bash\nuser3:x:1005:1005::\/home\/user3:\/bin\/bash\nbash:x:1006:1006::\/home\/bash:\/bin\/bash\nbasher:x:1007:1007::\/home\/basher:\/bin\/bash\nnologin:x:1008:1008::\/home\/nologin:\/sbin\/nologin\ntestbash:x:1009:1009::\/home\/testbash:\/bin\/bash\n\n[root@VM-4-10-centos data]# grep -E \"^([^:]+\\>).*\\1$\" \/etc\/passwd\nsync:x:5:0:sync:\/sbin:\/bin\/sync\nshutdown:x:6:0:shutdown:\/sbin:\/sbin\/shutdown\nhalt:x:7:0:halt:\/sbin:\/sbin\/halt\nbash:x:1006:1006::\/home\/bash:\/bin\/bash\nnologin:x:1008:1008::\/home\/nologin:\/sbin\/nologin<\/code><\/pre>\n<h2>fgrep<\/h2>\n<p>\u4e0d\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\u5143\u5b57\u7b26\uff0c\u5f53\u65e0\u9700\u8981\u7528\u5230\u5143\u5b57\u7b26\u53bb\u7f16\u5199\u6a21\u5f0f\u65f6\uff0c\u4f7f\u7528fgrep\u6027\u80fd\u66f4\u597d<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u56de\u987e\uff1a bash\u7684\u7279\u6027\uff1ahash,\u53d8\u91cf \u547d\u4ee4hash\uff1a hash\u547d\u4ee4 \u53d8\u91cf\uff1a \u672c\u5730\u53d8\u91cf\uff08\u4f5c\u7528\u57df\u4e3a\u5f53\u524dshell\u8fdb\u7a0b\uff09 \u73af\u5883\u53d8\u91cf\uff08\u5f53\u524dsh   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[131],"tags":[174,175,173,15],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8796"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8796"}],"version-history":[{"count":4,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8796\/revisions"}],"predecessor-version":[{"id":8800,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8796\/revisions\/8800"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8796"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}