{"id":415,"date":"2021-07-28T17:52:57","date_gmt":"2021-07-28T09:52:57","guid":{"rendered":"https:\/\/egonlin.com\/?p=415"},"modified":"2024-05-09T21:37:47","modified_gmt":"2024-05-09T13:37:47","slug":"%e7%ac%ac%e4%b8%80%e8%8a%82%ef%bc%9agrep%e5%91%bd%e4%bb%a4%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=415","title":{"rendered":"\u7b2c\u4e00\u8282\uff1agrep\u547d\u4ee4+\u6b63\u5219\u8868\u8fbe\u5f0f"},"content":{"rendered":"<h1>\u4e09\u5251\u5ba2\u4e4bgrep\u547d\u4ee4<\/h1>\n<h2>\u4e00 grep\u4ecb\u7ecd<\/h2>\n<p>grep\u547d\u4ee4\u4e3b\u8981\u7528\u4e8e\u8fc7\u6ee4\u6587\u672c\uff0cgrep\u5bb6\u65cf\u5982\u4e0b<\/p>\n<pre><code class=\"language-python\">grep\uff1a \u5728\u6587\u4ef6\u4e2d\u5168\u5c40\u67e5\u627e\u6307\u5b9a\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5e76\u6253\u5370\u6240\u6709\u5305\u542b\u8be5\u8868\u8fbe\u5f0f\u7684\u884c\r\negrep\uff1a\u6269\u5c55\u7684egrep\uff0c\u652f\u6301\u66f4\u591a\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5143\u5b57\u7b26\r\nfgrep\uff1a\u56fa\u5b9agrep(fixed grep)\uff0c\u6709\u65f6\u4e5f\u88ab\u79f0\u4f5c\u5feb\u901f(fast grep)\uff0c\u610f\u5473\u7740\u5b83\u53ea\u5904\u7406\u56fa\u5b9a\u5b57\u7b26\u4e32\u5373\u6309\u5b57\u9762\u89e3\u91ca\u6240\u6709\u7684\u5b57\u7b26\uff0c\u800c\r\n\u4e0d\u662f\u6b63\u5e38\u8868\u8fbe\u5f0f\uff0c\u800cgrep\u4e0eegrep\u662f\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\uff0c\u73b0\u5728\u5f88\u591a\u7cfb\u7edf\u4e2dfgrep\u5df2\u7ecf\u88abgrep -F\u66ff\u4ee3\uff0c\u4e8c\u8005\u7684\u529f\u80fd\u5b8c\u5168\u4e00\u6837<\/code><\/pre>\n<p>grep\u547d\u4ee4\u683c\u5f0f\u5982\u4e0b<\/p>\n<pre><code class=\"language-python\">grep [\u9009\u9879] PATTERN \u6587\u4ef61 \u6587\u4ef62 ...\r\n\r\n[root@egon ~]# grep 'root' \/etc\/passwd\r\n[root@egon ~]# fgrep 'bash' \/etc\/passwd\r\n\r\n\u627e\u5230\uff1a             grep\u8fd4\u56de\u7684\u9000\u51fa\u72b6\u6001\u4e3a0\r\n\u6ca1\u627e\u5230\uff1a            grep\u8fd4\u56de\u7684\u9000\u51fa\u72b6\u6001\u4e3a1\r\n\u627e\u4e0d\u5230\u6307\u5b9a\u6587\u4ef6\uff1a      grep\u8fd4\u56de\u7684\u9000\u51fa\u72b6\u6001\u4e3a2<\/code><\/pre>\n<p>grep \u547d\u4ee4\u7684\u8f93\u5165\u53ef\u4ee5\u6765\u81ea\u6807\u51c6\u8f93\u5165\u6216\u7ba1\u9053\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u6587\u4ef6\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">ps aux |grep '[n]ginx'\r\n<span style=\"background-color: #ff0000;\">\u4e3a\u4f55\u4e0d\u80fd\u76f4\u63a5ps aux |grep 'nginx'\u5462\uff0c\u56e0\u4e3a\u4e0d\u7528[]\u5305\u542b\uff0c\u90a3\u56e0\u4e3a\u4f60\u8fd9\u6761\u547d\u4ee4\u672c\u8eab\u5305\u542bnginx\u5b57\u7b26<\/span>\r\n<span style=\"background-color: #ff0000;\">\u6240\u4ee5\u4e5f\u4f1a\u88ab\u8fc7\u6ee4\u51fa\u6765\uff0c\u8fc7\u6ee4\u7684\u72b6\u6001\u7801\u6c38\u8fdc\u4e3a0<\/span><\/code><\/pre>\n<h2>\u4e8c \u9009\u9879<\/h2>\n<pre><code class=\"language-python\">-n, --line-number           \u5728\u8fc7\u6ee4\u51fa\u7684\u6bcf\u4e00\u884c\u524d\u9762\u52a0\u4e0a\u5b83\u5728\u6587\u4ef6\u4e2d\u7684\u76f8\u5bf9\u884c\u53f7\r\n-o, --only-matching         \u53ea\u663e\u793a\u5339\u914d\u7684\u5185\u5bb9\r\n-q, --quiet, --silent       \u9759\u9ed8\u6a21\u5f0f\uff0c\u6ca1\u6709\u4efb\u4f55\u8f93\u51fa\uff0c\u5f97\u7528$?\u6765\u5224\u65ad\u6267\u884c\u6210\u529f\u6ca1\u6709\uff0c\u5373\u6709\u6ca1\u6709\u8fc7\u6ee4\u5230\u60f3\u8981\u7684\u5185\u5bb9\r\n--color                     \u989c\u8272\r\n-i, --ignore-case           \u5ffd\u7565\u5927\u5c0f\u5199\r\n-A, --after-context=NUM     \u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u5219\u5c06\u5339\u914d\u884c\u53ca\u5176\u540en\u884c\u4e00\u8d77\u6253\u5370\u51fa\u6765\r\n-B, --before-context=NUM    \u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u5219\u5c06\u5339\u914d\u884c\u53ca\u5176\u524dn\u884c\u4e00\u8d77\u6253\u5370\u51fa\u6765\r\n-C, --context=NUM           \u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u5219\u5c06\u5339\u914d\u884c\u53ca\u5176\u524d\u540en\u884c\u4e00\u8d77\u6253\u5370\u51fa\u6765\r\n-c, --count                 \u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u5219\u5c06\u5339\u914d\u5230\u7684\u884c\u6570\u6253\u5370\u51fa\u6765\r\n-v, --invert-match          \u53cd\u5411\u67e5\u627e\uff0c\u53ea\u663e\u793a\u4e0d\u5339\u914d\u7684\u884c\r\n-w                          \u5339\u914d\u5355\u8bcd\r\n-E                          \u7b49\u4e8eegrep\uff0c\u6269\u5c55\r\n\r\n-l, --files-with-matches    \u5982\u679c\u5339\u914d\u6210\u529f\uff0c\u5219\u53ea\u5c06\u6587\u4ef6\u540d\u6253\u5370\u51fa\u6765\uff0c\u5931\u8d25\u5219\u4e0d\u6253\u5370\r\n                            \u901a\u5e38-rl\u4e00\u8d77\u7528\uff0cgrep -rl 'root' \/etc \r\n-R, -r, --recursive         \u9012\u5f52<\/code><\/pre>\n<p>\u793a\u4f8b<\/p>\n<pre><code class=\"language-python\"># 1\u3001-n\r\n[root@egon ~]# grep -n 'root' \/etc\/passwd\r\n1:root:x:0:0:root:\/root:\/bin\/bash\r\n10:operator:x:11:0:operator:\/root:\/sbin\/nologin\r\n[root@egon ~]# \r\n\r\n# 2\u3001-o\r\n[root@egon ~]# grep -o 'root' \/etc\/passwd\r\nroot\r\nroot\r\nroot\r\nroot\r\n[root@egon ~]# \r\n\r\n# 3\u3001-q\r\n[root@egon ~]# grep -q 'root' \/etc\/passwd \r\n[root@egon ~]# echo $?\r\n0\r\n\r\n# 4\u3001--color\r\n[root@egon ~]# alias grep\r\nalias grep='grep --color=auto'\r\n[root@egon ~]# \r\n\r\n# 5\u3001-i\r\n[root@egon ~]# echo \"EGON\" |grep -i egon \r\nEGON\r\n[root@egon ~]# \r\n\r\n# 6\u3001-A\\-B\\-C\r\n[root@egon ~]# grep -A 2 'root' \/etc\/passwd\r\n[root@egon ~]# grep -B 2 'root' \/etc\/passwd\r\n[root@egon ~]# grep -C 2 'root' \/etc\/passwd\r\n\r\n# 7\u3001-c\r\n[root@egon ~]# grep -c 'root' \/etc\/passwd\r\n2\r\n[root@egon ~]# \r\n\r\n# 8\u3001-v\r\n[root@egon ~]# ps aux | grep nginx |grep -v grep\r\n[root@egon ~]# \r\n[root@egon ~]# ps aux | grep [n]ginx\r\n[root@egon ~]# \r\n\r\n# 9\u3001-w\r\n[root@egon ~]# netstat -an |grep -w 80\r\ntcp6       0      0 :::80                   :::*                    LISTEN    \r\n[root@egon ~]# netstat -an |grep  '\\&lt;80\\&gt;'\r\ntcp6       0      0 :::80                   :::*                    LISTEN    \r\n[root@egon ~]# netstat -an |grep  '\\b80\\b'\r\ntcp6       0      0 :::80                   :::*                    LISTEN   \r\n\r\n# 10\u3001-rl\r\n[root@egon ~]# grep -rl 'root' \/etc  # \u5c06\/etc\u76ee\u5f55\u4e0b\u6240\u6709\u5305\u542b'root'\u5185\u5bb9\u7684\u6587\u4ef6\u90fd\u5217\u51fa\u6765<\/code><\/pre>\n<h2>\u4e09 \u6b63\u5219\u8868\u8fbe\u5f0f<\/h2>\n<h3>3.1 \u6b63\u5219\u8868\u8fbe\u5f0f\u4ecb\u7ecd<\/h3>\n<p><strong>\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\uff0c\u53c8\u79f0\u89c4\u5219\u8868\u8fbe\u5f0f<strong>\u3002<\/strong>\uff08\u82f1\u8bed\uff1aRegular Expression\uff0c\u5728\u4ee3\u7801\u4e2d\u5e38\u7b80\u5199\u4e3aregex\u3001regexp\u6216RE\uff09\uff0c\u662f\u8ba1\u7b97\u673a\u79d1\u5b66\u7684\u4e00\u4e2a\u6982\u5ff5\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u7531\u5143\u5b57\u7b26\u7ec4\u6210\uff0c\u901a\u5e38\u88ab\u7528\u6765\u68c0\u7d22\u3001\u66ff\u6362\u90a3\u4e9b\u7b26\u5408\u67d0\u4e2a\u6a21\u5f0f(\u89c4\u5219)\u7684\u6587\u672c\uff08\u8bb8\u591a\u7a0b\u5e8f\u8bbe\u8ba1\u8bed\u8a00\u90fd\u652f\u6301\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5b57\u7b26\u4e32\u64cd\u4f5c\uff09\u3002<\/p>\n<p>\u5143\u5b57\u7b26\uff1a\u662f\u4e00\u7c7b\u53ef\u4ee5\u8868\u8fbe\u51fa\u8d85\u8d8a\u5176\u5b57\u9762\u672c\u8eab\u542b\u4e49\u7684\u7279\u6b8a\u5b57\u7b26<\/p>\n<pre><code class=\"language-python\">shell\u5143\u5b57\u7b26(\u4e5f\u79f0\u4e3a\u901a\u914d\u7b26)\uff1a \u7531shell\u89e3\u91ca\u5668\u6765\u89e3\u6790\uff0c\u5982rm -rf *.pdf\uff0c\u5143\u5b57\u7b26*Shell\u5c06\u5176\u89e3\u6790\u4e3a\u4efb\u610f\u591a\u4e2a\u5b57\u7b26\r\n\u6b63\u5219\u8868\u8fbe\u5f0f\u5143\u5b57\u7b26         \uff1a \u7531\u5404\u79cd\u6267\u884c\u6a21\u5f0f\u5339\u914d\u64cd\u4f5c\u7684\u7a0b\u5e8f\u6765\u89e3\u6790\uff0c\u6bd4\u5982vi\u3001grep\u3001sed\u3001awk\r\n\r\n\u4f8b\u5982\uff1avim\u793a\u4f8b\uff1a\r\n:1,$ s\/tom\/EGON\/g        # \u5982anatomy\u3001tomatoes\u53catomorrow\u4e2d\u7684\u201ctom\u201d\u88ab\u66ff\u6362\u4e86\uff0c\u800cTom\u786e\u6ca1\u88ab\u66ff\u6362\r\n:1,$ s\/\\&lt;[Tt]om\\&gt;\/EGON\/g<\/code><\/pre>\n<h3>3.2 \u6b63\u5219\u8868\u8fbe\u5f0f\u5143\u5b57\u7b26<\/h3>\n<h4>3.2.1 \u57fa\u672c\u6b63\u5219\u5143\u5b57\u7b26\u96c6<\/h4>\n<pre><code class=\"language-python\">\u5143\u5b57\u7b26         \u529f\u80fd                                        \u793a\u4f8b    \r\n^             \u884c\u9996                                        ^love\r\n$             \u884c\u5c3e                                         love$\r\n.             \u9664\u4e86\u6362\u884c\u7b26\u4ee5\u5916\u7684\u4efb\u610f\u5355\u4e2a\u5b57\u7b26                     l..e\r\n*             \u524d\u5bfc\u5b57\u7b26\u7684\u96f6\u4e2a\u6216\u591a\u4e2a                            ab*love\r\n.*            \u6240\u6709\u5b57\u7b26                                      a.*love\r\n[]            \u5b57\u7b26\u7ec4\u5185\u7684\u4efb\u4e00\u5b57\u7b26                              [lL]ove\r\n[^]           \u5bf9\u5b57\u7b26\u7ec4\u5185\u7684\u6bcf\u4e2a\u5b57\u7b26\u53d6\u53cd(\u4e0d\u5339\u914d\u5b57\u7b26\u7ec4\u5185\u7684\u6bcf\u4e2a\u5b57\u7b26)   [^a-z0-9]ove\r\n^[^]          \u975e\u5b57\u7b26\u7ec4\u5185\u7684\u5b57\u7b26\u5f00\u5934\u7684\u884c\r\n\r\n[a-z]         \u5c0f\u5199\u5b57\u6bcd\r\n[A-Z]         \u5927\u5199\u5b57\u6bcd\r\n[a-Z]         \u5c0f\u5199\u548c\u5927\u5199\u5b57\u6bcd\r\n[0-9]         \u6570\u5b57\r\n\r\n\\            \u7528\u6765\u8f6c\u4e49\u5143\u5b57\u7b26                                      love\\.    \r\n\\&lt;            \u8bcd\u9996\u5b9a\u4f4d\u7b26 \u5355\u8bcd\u4e00\u822c\u4ee5\u7a7a\u683c\u6216\u7279\u6b8a\u5b57\u7b26\u505a\u5206\u9694\u3001\u8fde\u7eed\u7684\u5b57\u7b26\u7ec4\u6210  \\&lt;love\r\n\\&gt;            \u8bcd\u5c3e\u5b9a\u4f4d\u7b26                                         love\\&gt;\r\n\\(..\\)        \u5339\u914d\u7a0d\u540e\u5c06\u8981\u4f7f\u7528\u7684\u5b57\u7b26\u7684\u6807\u7b7e                    \\(love\\)able\\1er    \r\n                                                         :1,$ s\/\\(192.168.11\\).66\/\\1.50\/g\r\n\r\nx\\{m\\}          \u5b57\u7b26x\u91cd\u590d\u51fa\u73b0m\u6b21                                e\\{3\\}\r\nx\\{m,\\}         \u5b57\u7b26x\u91cd\u590d\u51fa\u73b0m\u6b21\u4ee5\u4e0a                             e\\{3,\\}                     \r\nx\\{m,n\\}        \u5b57\u7b26x\u91cd\u590d\u51fa\u73b0m\u5230n\u6b21                             e\\{3,6\\}    <\/code><\/pre>\n<p>\u793a\u4f8b<\/p>\n<pre><code class=\"language-python\"># 1\u3001^ \u884c\u9996\r\n[root@egon ~]# grep '^root' \/etc\/passwd\r\nroot:x:0:0:root:\/root:\/bin\/bash\r\n[root@egon ~]# \r\n\r\n# 2\u3001$ \u884c\u5c3e\r\n[root@egon ~]# grep 'bash$' \/etc\/passwd\r\nroot:x:0:0:root:\/root:\/bin\/bash\r\nuser1:x:1002:1003::\/home\/user1:\/bin\/bash\r\negon1:x:198:1005::\/home\/egon1:\/bin\/bash\r\ngg:x:1004:1006::\/home\/gg:\/bin\/bash\r\negon:x:1005:1007::\/home\/egon:\/bin\/bash\r\ntom:x:1006:1008::\/home\/tom:\/bin\/bash\r\n[root@egon ~]# \r\n\r\n# 3\u3001. \u9664\u4e86\u6362\u884c\u7b26\u4ee5\u5916\u7684\u4efb\u610f\u5355\u4e2a\u5b57\u7b26\r\n[root@egon ~]# grep 'r..t' \/etc\/passwd\r\nroot:x:0:0:root:\/root:\/bin\/bash\r\noperator:x:11:0:operator:\/root:\/sbin\/nologin\r\nftp:x:14:50:FTP User:\/var\/ftp:\/sbin\/nologin\r\n[root@egon ~]# \r\n\r\n# 4\u3001* \u524d\u5bfc\u5b57\u7b26\u7684\u96f6\u4e2a\u6216\u591a\u4e2a\r\n[root@egon ~]# cat a.txt \r\na\r\n\r\nab\r\nabb\r\nabbb\r\nbbbbb\r\n[root@egon ~]# grep 'ab*' a.txt \r\na\r\nab\r\nabb\r\nabbb\r\n[root@egon ~]# \r\n\r\n# 5\u3001.* \u6240\u6709\u5b57\u7b26=\u8d2a\u5a6a\r\n[root@egon ~]# cat a.txt \r\na123+-*\/c11113333c\r\na1c\r\na77Ac\r\na23333c\r\nac\r\n111\r\n222\r\n333\r\n[root@egon ~]# grep 'a.*c' a.txt \r\na123+-*\/c11113333c\r\na1c\r\na77Ac\r\na23333c\r\nac\r\n[root@egon ~]# \r\n\r\n# 5.1 .*?=\u300b\u975e\u8d2a\u5a6a\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cgrep\u4e0d\u652f\u6301\u975e\u8d2a\u5a6a\u4fee\u9970\u7b26\uff0c\u4f46\u60a8\u53ef\u4ee5\u4f7f\u7528grep -P\u6765\u4f7f\u7528Perl\u8bed\u6cd5\u6765\u652f\u6301.*?\r\n[root@egon ~]# cat a.txt \r\n&lt;a href=\"http:\/\/www.baidu.com\"&gt;\"\u6211\u4ed6\u5988\u7684\u662f\u767e\u5ea6\"&lt;\/a&gt;\r\n&lt;a href=\"http:\/\/www.sina.com.cn\"&gt;\"\u6211\u7279\u4e48\u7684\u662f\u65b0\u6d6a\"&lt;\/a&gt;\r\n[root@egon ~]# \r\n\r\n[root@egon ~]# grep -o 'href=\".*\"' a.txt  # \u8d2a\u5a6a\r\nhref=\"http:\/\/www.baidu.com\"&gt;\"\u6211\u4ed6\u5988\u7684\u662f\u767e\u5ea6\"\r\nhref=\"http:\/\/www.sina.com.cn\"&gt;\"\u6211\u7279\u4e48\u7684\u662f\u65b0\u6d6a\"\r\n[root@egon ~]# \r\n\r\n[root@egon ~]# grep -oP 'href=\".*?\"' a.txt  # \u975e\u8d2a\u5a6a\r\nhref=\"http:\/\/www.baidu.com\"\r\nhref=\"http:\/\/www.sina.com.cn\"\r\n[root@egon ~]# \r\n\r\n# 6\u3001[] \u5b57\u7b26\u7ec4\u5185\u7684\u4efb\u4e00\u5b57\u7b26\r\n# 7\u3001[^] \u5bf9\u5b57\u7b26\u7ec4\u5185\u7684\u6bcf\u4e2a\u5b57\u7b26\u53d6\u53cd(\u4e0d\u5339\u914d\u5b57\u7b26\u7ec4\u5185\u7684\u6bcf\u4e2a\u5b57\u7b26)\r\n[root@egon ~]# cat a.txt \r\na1c\r\na2c\r\na33c\r\naAc\r\naZc\r\n[root@egon ~]# grep 'a[0-9]c' a.txt\r\na1c\r\na2c\r\n[root@egon ~]# grep 'a[^0-9]c' a.txt\r\naAc\r\naZc\r\n[root@egon ~]# \r\n[root@egon ~]# grep 'a[0-9][0-9]c' a.txt\r\na33c\r\n[root@egon ~]# \r\n\r\n# 8\u3001^[^] \u975e\u5b57\u7b26\u7ec4\u5185\u7684\u5b57\u7b26\u5f00\u5934\u7684\u884c\r\n[root@egon ~]# cat a.txt \r\na1c\r\na2c\r\na33c\r\naAc\r\naZc\r\n[root@egon ~]# grep '^[^0-9]..$' a.txt\r\na1c\r\na2c\r\naAc\r\naZc\r\n[root@egon ~]# \r\n\r\n# 9\u3001[a-z] \u5c0f\u5199\u5b57\u6bcd\r\n# 10\u3001[A-Z] \u5927\u5199\u5b57\u6bcd\r\n# 11\u3001[a-Z] \u5c0f\u5199\u548c\u5927\u5199\u5b57\u6bcd\r\n# 12\u3001[0-9] \u6570\u5b57\r\n\r\n# 13\u3001\\&lt; \u5355\u8bcd\u5934 \u5355\u8bcd\u4e00\u822c\u4ee5\u7a7a\u683c\u6216\u7279\u6b8a\u5b57\u7b26\u505a\u5206\u9694,\u8fde\u7eed\u7684\u5b57\u7b26\u4e32\u88ab\u5f53\u505a\u5355\u8bcd\r\n# 14\u3001\\&gt; \u5355\u8bcd\u5c3e\r\n[root@egon ~]# netstat -an |grep -w 80\r\ntcp6       0      0 :::80                   :::*                    LISTEN    \r\n[root@egon ~]# netstat -an |grep  '\\&lt;80\\&gt;'\r\ntcp6       0      0 :::80                   :::*                    LISTEN    \r\n[root@egon ~]# netstat -an |grep  '\\b80\\b'\r\ntcp6       0      0 :::80                   :::*                    LISTEN   <\/code><\/pre>\n<p>Ps: grep\u5339\u914d\u6362\u884c\u7b26\u548c\u5236\u8868\u7b26\uff08<strong><span style=\"background-color: #ff0000;\">\u4e0b\u8ff0\u6a21\u7248\u6587\u4ef6\u62f7\u8d1d\u7684\u65f6\u5019\u5236\u8868\u7b26\u4f1a\u4e22\u5931\uff0c\u4f60\u9700\u8981\u5728\u4f60\u7684\u6587\u4ef6\u91cc\u624b\u52a8\u5199\u4e00\u4e0b\u5236\u8868\u7b26<\/span><\/strong>\uff09<\/p>\n<pre><code class=\"language-python\">[root@centos-7 ~]# cat a.txt \r\neg\r\non1\r\negon123\r\n111on222on333eg\r\n666666\r\negon\r\n[root@centos-7 ~]# grep -Pz \"eg\\non\" a.txt  # -P\u4f7f\u7528Perl\u6b63\u5219\u8868\u8fbe\u5f0f -z\u5904\u7406\u591a\u884c\r\neg--------&gt;\u9ad8\u4eae\r\non1-------&gt;\u9ad8\u4eae\r\neg  on123\r\n111on222on333eg\r\n666666\r\negon\r\n[root@centos-7 ~]# grep -Pzo \"eg\\non\" a.txt  # -P\u4f7f\u7528Perl\u6b63\u5219\u8868\u8fbe\u5f0f -z\u5904\u7406\u591a\u884c\r\neg\r\non\r\n[root@centos-7 ~]# \r\n[root@centos-7 ~]# grep -Pz \"eg\\ton\" a.txt  # -P\u4f7f\u7528Perl\u6b63\u5219\u8868\u8fbe\u5f0f -z\u5904\u7406\u591a\u884c\r\neg\r\non1\r\neg  on123-----&gt;\u9ad8\u4eae\uff0c\u6ce8\u610f\uff0c\u4f60\u62f7\u8d1d\u8fd9\u4e2a\u6587\u6863\u7684\u65f6\u5019\u53ef\u80fd\u65e0\u6cd5\u5e26\u4e0atab\u952e\uff0c\u62f7\u8d1d\u5230\u76ee\u6807\u6587\u4ef6\u65f6\u81ea\u5df1\u624b\u52a8\u7f16\u8f91\u4e00\u4e0b\u52a0\u4e0atab\r\n111on222on333eg\r\n666666\r\negon\r\n[root@centos-7 ~]# grep -Pzo \"eg\\ton\" a.txt  # -P\u4f7f\u7528Perl\u6b63\u5219\u8868\u8fbe\u5f0f -z\u5904\u7406\u591a\u884c\r\neg  on-----&gt;\u9ad8\u4eae\r\n[root@centos-7 ~]# <\/code><\/pre>\n<h4>3.2.2 \u6269\u5c55\u6b63\u5219\u5143\u5b57\u7b26\u96c6<\/h4>\n<div id=\"rml_readmorelogin_placeholder\" style=\"position:relative;\"><div id=\"rml_fade_content\" style=\"position: absolute;\r\ntop:-10em;\r\nwidth:100%;\r\nheight:10em;\r\nbackground: -webkit-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -moz-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -o-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -ms-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\"><\/div><div class=\"wpf-controller aru_rml_from_in_post\" style=\"background-color:#eeeeee;border:5px solid #cce6ff;\" id=\"ARU_ReadMoreLogin_ReadMoreLoginController\"><h2 id=\"Header\">\u67e5\u770b\u66f4\u591a<\/h2><div id=\"Message\"><p>\u8054\u7cfb\u7ba1\u7406\u5458\u5fae\u4fe1tutu19192010\uff0c\u6ce8\u518c\u8d26\u53f7<\/p>\n<\/div><div id=\"StatusBarHeader\"><\/div><form id=\"ARU_ReadMoreLogin_ReadMoreLoginController\"><input name=\"post_id\" value=\"415\" type=\"hidden\"\/><input name=\"_init_callback\" value=\"InitLogin\" type=\"hidden\"\/><input name=\"post_id\" value=\"415\" type=\"hidden\"\/><input name=\"rt_ype\" value=\"1\" type=\"hidden\"\/><input name=\"nonce\" value=\"2bb7c47a30\" type=\"hidden\"\/><input name=\"_wpnonce\" value=\"2f39b0f723\" type=\"hidden\"\/><input name=\"_controller\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginController\" type=\"hidden\"\/><input name=\"_proxy_controller\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginController\" type=\"hidden\"\/><input name=\"_view\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginView\" type=\"hidden\"\/><table class=\"wpf-table-placeholder\"><tbody class=\"wpf-table-placeholder\"><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\" width=\"400px\"><table class=\"wpf-table-placeholder\"><tbody class=\"wpf-table-placeholder\"><tr class=\"wpf-table-placeholder\"><th class=\"wpf-table-placeholder-input\"><label class=\"wpf-label\">Username:<\/label><\/th><\/tr><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\"><input class=\"regular-text text_input\" name=\"username\" value=\"\" type=\"text\"\/><\/td><\/tr><tr class=\"wpf-table-placeholder\"><th class=\"wpf-table-placeholder-input\"><label class=\"wpf-label\">Password:<\/label><\/th><\/tr><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\"><input class=\"regular-text text_input\" name=\"password\" value=\"\" type=\"password\"\/><\/td><\/tr><\/tbody><\/table><p class=\"wpf-table-placeholder submit\"><button class=\"wp_plugin_framework_ajax_button\" type=\"button\" style=\"background-color:#4D90FE;;color:#ffffff;;border:1px solid #3079ed;\" name=\"_event\" value=\"ButtonLogin\">Log in<\/button><\/p><\/td><td class=\"wpf-table-placeholder-input\"><\/td><\/tr><\/tbody><\/table><\/form><div id=\"ButtonStartRegister\"><a href=\"https:\/\/egonlin.com\/wp-login.php?action=register\">Register<\/a><\/div><div id=\"Link1\"><a href=\"https:\/\/egonlin.com\/wp-login.php?action=lostpassword\">Forgotten username or password?<\/a><\/div><div id=\"StatusBarFooter\"><\/div><\/div><\/div><div id=aru_remaining_content><\/div>","protected":false},"excerpt":{"rendered":"<p>\u4e09\u5251\u5ba2\u4e4bgrep\u547d\u4ee4 \u4e00 grep\u4ecb\u7ecd grep\u547d\u4ee4\u4e3b\u8981\u7528\u4e8e\u8fc7\u6ee4\u6587\u672c\uff0cgrep\u5bb6\u65cf\u5982\u4e0b grep\uff1a \u5728\u6587\u4ef6\u4e2d\u5168 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[70],"tags":[21,19,75,22,20],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/415"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=415"}],"version-history":[{"count":5,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/415\/revisions"}],"predecessor-version":[{"id":10846,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/415\/revisions\/10846"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}