mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-12-17 16:34:32 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
<h2 id="补充说明">补充说明</h2>
|
||||
<p><strong>grep</strong> (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。用于过滤/搜索的特定字符。可使用正则表达式能多种命令配合使用,使用上十分灵活。</p>
|
||||
<h3 id="选项">选项</h3>
|
||||
<div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="ex">-a</span> --text # 不要忽略二进制数据。</a>
|
||||
<div class="sourceCode" id="cb1"><pre><code class="language-bash"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="ex">-a</span> --text # 不要忽略二进制数据。</a>
|
||||
<a class="sourceLine" id="cb1-2" data-line-number="2"><span class="ex">-A</span> <span class="op"><</span>显示行数<span class="op">></span> --after-context=<span class="op"><</span>显示行数<span class="op">></span> # 除了显示符合范本样式的那一行之外,并显示该行之后的内容。</a>
|
||||
<a class="sourceLine" id="cb1-3" data-line-number="3"><span class="ex">-b</span> --byte-offset # 在显示符合范本样式的那一行之外,并显示该行之前的内容。</a>
|
||||
<a class="sourceLine" id="cb1-4" data-line-number="4"><span class="ex">-B</span><span class="op"><</span>显示行数<span class="op">></span> --before-context=<span class="op"><</span>显示行数<span class="op">></span> # 除了显示符合样式的那一行之外,并显示该行之前的内容。</a>
|
||||
@@ -31,7 +31,7 @@
|
||||
<a class="sourceLine" id="cb1-26" data-line-number="26"><span class="ex">-y</span> <span class="co"># 此参数效果跟“-i”相同。</span></a>
|
||||
<a class="sourceLine" id="cb1-27" data-line-number="27"><span class="ex">-o</span> <span class="co"># 只输出文件中匹配到的部分。</span></a></code></pre></div>
|
||||
<h3 id="规则表达式">规则表达式</h3>
|
||||
<div class="sourceCode" id="cb2"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb2-1" data-line-number="1">^ # 锚定行的开始 如:<span class="st">'^grep'</span>匹配所有以<span class="ex">grep</span>开头的行。 </a>
|
||||
<div class="sourceCode" id="cb2"><pre><code class="language-bash"><a class="sourceLine" id="cb2-1" data-line-number="1">^ # 锚定行的开始 如:<span class="st">'^grep'</span>匹配所有以<span class="ex">grep</span>开头的行。 </a>
|
||||
<a class="sourceLine" id="cb2-2" data-line-number="2">$ # 锚定行的结束 如:<span class="st">'grep$'</span>匹配所有以<span class="ex">grep</span>结尾的行。 </a>
|
||||
<a class="sourceLine" id="cb2-3" data-line-number="3"><span class="bu">.</span> # 匹配一个非换行符的字符 如:<span class="st">'gr.p'</span>匹配<span class="ex">gr</span>后接一个任意字符,然后是p。 </a>
|
||||
<a class="sourceLine" id="cb2-4" data-line-number="4"><span class="ex">*</span> # 匹配零个或多个先前字符 如:<span class="st">'*grep'</span>匹配所有一个或多个空格后紧跟grep的行。 </a>
|
||||
@@ -49,35 +49,35 @@
|
||||
<a class="sourceLine" id="cb2-16" data-line-number="16">\<span class="ex">b</span> # 单词锁定符,如: <span class="st">'\bgrep\b'</span>只匹配grep。 </a></code></pre></div>
|
||||
<h2 id="grep命令常见用法">grep命令常见用法</h2>
|
||||
<p>在文件中搜索一个单词,命令会返回一个包含 <strong>“match_pattern”</strong> 的文本行:</p>
|
||||
<pre><code>grep match_pattern file_name
|
||||
<pre><code class="language-bash">grep match_pattern file_name
|
||||
grep "match_pattern" file_name
|
||||
</code></pre>
|
||||
<p>在多个文件中查找:</p>
|
||||
<pre><code>grep "match_pattern" file_1 file_2 file_3 ...
|
||||
<pre><code class="language-bash">grep "match_pattern" file_1 file_2 file_3 ...
|
||||
</code></pre>
|
||||
<p>输出除之外的所有行 <strong>-v</strong> 选项:</p>
|
||||
<pre><code>grep -v "match_pattern" file_name
|
||||
<pre><code class="language-bash">grep -v "match_pattern" file_name
|
||||
</code></pre>
|
||||
<p>标记匹配颜色 <strong>–color=auto</strong> 选项:</p>
|
||||
<pre><code>grep "match_pattern" file_name --color=auto
|
||||
<pre><code class="language-bash">grep "match_pattern" file_name --color=auto
|
||||
</code></pre>
|
||||
<p>使用正则表达式 <strong>-E</strong> 选项:</p>
|
||||
<pre><code>grep -E "[1-9]+"
|
||||
<pre><code class="language-bash">grep -E "[1-9]+"
|
||||
或
|
||||
egrep "[1-9]+"
|
||||
</code></pre>
|
||||
<p>只输出文件中匹配到的部分 <strong>-o</strong> 选项:</p>
|
||||
<pre><code>echo this is a test line. | grep -o -E "[a-z]+\."
|
||||
<pre><code class="language-bash">echo this is a test line. | grep -o -E "[a-z]+\."
|
||||
line.
|
||||
|
||||
echo this is a test line. | egrep -o "[a-z]+\."
|
||||
line.
|
||||
</code></pre>
|
||||
<p>统计文件或者文本中包含匹配字符串的行数 <strong>-c</strong> 选项:</p>
|
||||
<pre><code>grep -c "text" file_name
|
||||
<pre><code class="language-bash">grep -c "text" file_name
|
||||
</code></pre>
|
||||
<p>输出包含匹配字符串的行数 <strong>-n</strong> 选项:</p>
|
||||
<pre><code>grep "text" -n file_name
|
||||
<pre><code class="language-bash">grep "text" -n file_name
|
||||
或
|
||||
cat file_name | grep "text" -n
|
||||
|
||||
@@ -85,25 +85,25 @@ cat file_name | grep "text" -n
|
||||
grep "text" -n file_1 file_2
|
||||
</code></pre>
|
||||
<p>打印样式匹配所位于的字符或字节偏移:</p>
|
||||
<pre><code>echo gun is not unix | grep -b -o "not"
|
||||
<pre><code class="language-bash">echo gun is not unix | grep -b -o "not"
|
||||
7:not
|
||||
|
||||
#一行中字符串的字符便宜是从该行的第一个字符开始计算,起始值为0。选项 **-b -o** 一般总是配合使用。
|
||||
</code></pre>
|
||||
<p>搜索多个文件并查找匹配文本在哪些文件中:</p>
|
||||
<pre><code>grep -l "text" file1 file2 file3...
|
||||
<pre><code class="language-bash">grep -l "text" file1 file2 file3...
|
||||
</code></pre>
|
||||
<h3 id="grep递归搜索文件">grep递归搜索文件</h3>
|
||||
<p>在多级目录中对文本进行递归搜索:</p>
|
||||
<pre><code>grep "text" . -r -n
|
||||
<pre><code class="language-bash">grep "text" . -r -n
|
||||
# .表示当前目录。
|
||||
</code></pre>
|
||||
<p>忽略匹配样式中的字符大小写:</p>
|
||||
<pre><code>echo "hello world" | grep -i "HELLO"
|
||||
<pre><code class="language-bash">echo "hello world" | grep -i "HELLO"
|
||||
hello
|
||||
</code></pre>
|
||||
<p>选项 <strong>-e</strong> 制动多个匹配样式:</p>
|
||||
<pre><code>echo this is a text line | grep -e "is" -e "line" -o
|
||||
<pre><code class="language-bash">echo this is a text line | grep -e "is" -e "line" -o
|
||||
is
|
||||
line
|
||||
|
||||
@@ -115,7 +115,7 @@ bbb
|
||||
echo aaa bbb ccc ddd eee | grep -f patfile -o
|
||||
</code></pre>
|
||||
<p>在grep搜索结果中包括或者排除指定文件:</p>
|
||||
<pre><code>#只在目录中所有的.php和.html文件中递归搜索字符"main()"
|
||||
<pre><code class="language-bash">#只在目录中所有的.php和.html文件中递归搜索字符"main()"
|
||||
grep "main()" . -r --include *.{php,html}
|
||||
|
||||
#在搜索结果中排除所有README文件
|
||||
@@ -125,7 +125,7 @@ grep "main()" . -r --exclude "README"
|
||||
grep "main()" . -r --exclude-from filelist
|
||||
</code></pre>
|
||||
<p>使用0值字节后缀的grep与xargs:</p>
|
||||
<div class="sourceCode" id="cb17"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb17-1" data-line-number="1"><span class="co"># 测试文件:</span></a>
|
||||
<div class="sourceCode" id="cb17"><pre><code class="language-bash"><a class="sourceLine" id="cb17-1" data-line-number="1"><span class="co"># 测试文件:</span></a>
|
||||
<a class="sourceLine" id="cb17-2" data-line-number="2"><span class="bu">echo</span> <span class="st">"aaa"</span> <span class="op">></span> file1</a>
|
||||
<a class="sourceLine" id="cb17-3" data-line-number="3"><span class="bu">echo</span> <span class="st">"bbb"</span> <span class="op">></span> file2</a>
|
||||
<a class="sourceLine" id="cb17-4" data-line-number="4"><span class="bu">echo</span> <span class="st">"aaa"</span> <span class="op">></span> file3</a>
|
||||
@@ -134,10 +134,10 @@ grep "main()" . -r --exclude-from filelist
|
||||
<a class="sourceLine" id="cb17-7" data-line-number="7"></a>
|
||||
<a class="sourceLine" id="cb17-8" data-line-number="8"><span class="co">#执行后会删除file1和file3,grep输出用-Z选项来指定以0值字节作为终结符文件名(\0),xargs -0 读取输入并用0值字节终结符分隔文件名,然后删除匹配文件,-Z通常和-l结合使用。</span></a></code></pre></div>
|
||||
<p>grep静默输出:</p>
|
||||
<div class="sourceCode" id="cb18"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb18-1" data-line-number="1"><span class="fu">grep</span> -q <span class="st">"test"</span> filename</a>
|
||||
<div class="sourceCode" id="cb18"><pre><code class="language-bash"><a class="sourceLine" id="cb18-1" data-line-number="1"><span class="fu">grep</span> -q <span class="st">"test"</span> filename</a>
|
||||
<a class="sourceLine" id="cb18-2" data-line-number="2"><span class="co"># 不会输出任何信息,如果命令运行成功返回0,失败则返回非0值。一般用于条件测试。</span></a></code></pre></div>
|
||||
<p>打印出匹配文本之前或者之后的行:</p>
|
||||
<div class="sourceCode" id="cb19"><pre class="sourceCode bash"><code class="sourceCode bash"><a class="sourceLine" id="cb19-1" data-line-number="1"><span class="co"># 显示匹配某个结果之后的3行,使用 -A 选项:</span></a>
|
||||
<div class="sourceCode" id="cb19"><pre><code class="language-bash"><a class="sourceLine" id="cb19-1" data-line-number="1"><span class="co"># 显示匹配某个结果之后的3行,使用 -A 选项:</span></a>
|
||||
<a class="sourceLine" id="cb19-2" data-line-number="2"><span class="fu">seq</span> 10 <span class="kw">|</span> <span class="fu">grep</span> <span class="st">"5"</span> -A 3</a>
|
||||
<a class="sourceLine" id="cb19-3" data-line-number="3"><span class="ex">5</span></a>
|
||||
<a class="sourceLine" id="cb19-4" data-line-number="4"><span class="ex">6</span></a>
|
||||
|
||||
Reference in New Issue
Block a user