pages tagged grep http://meng6net.localhost/tag/grep/ <p><small>Copyright © 2005-2020 by <code>Meng Lu &lt;lumeng3@gmail.com&gt;</code></small></p> Meng Lu's home page ikiwiki Tue, 16 May 2017 23:59:39 +0000 Examples of using grep http://meng6net.localhost/computing/example/examples_of_using_grep/ http://meng6net.localhost/computing/example/examples_of_using_grep/ ack computing example grep Tue, 16 May 2017 23:59:39 +0000 2017-05-16T23:59:39Z <h2>Find lines in <code>file1.txt</code> but not in <code>file2.txt</code></h2> <pre><code>grep -Fxvf file2.txt file1.txt </code></pre> <h2>Find lines containing CJK characters from a file</h2> <pre> <code>grep --color=auto -P '.*\([^\x00-\x7f][^\x00-\x7f][^\x00-\x7f][^\x00-\x7f][^\x00-\x7f][^\x00-\x7f]\).* file.txt </code></pre> <h2>Delete (really) empty lines</h2> <p>Delete (really) empty lines</p> <pre><code>grep . oldfile &gt; newfile </code></pre> <p>or</p> <pre><code>grep ^$ oldfile &gt; newfile </code></pre> <p>or, delete really empty lines and lines with only whitespace characters</p> <pre><code>grep -E ^[[:whitespace:]]*$ oldfile &gt; newfile </code></pre> <h2>Change <code>};</code> to <code>}</code> in multiple files</h2> <pre><code>grep -rl "};" *.m | xargs sed -i .bak -e 's/};/}/' </code></pre> <p>Remarks:</p> <p><code>grep</code>:</p> <ul> <li> <p><code>-r</code> recursively find</p> </li> <li> <p><code>-l</code> list files matching text</p> </li> <li> <p><code>xargs</code> construct argument list and execute utility</p> </li> </ul> <p><code>sed</code>:</p> <ul> <li> <p><code>-i .bak</code> change in place but keep backup with extension <code>.bak</code></p> </li> <li> <p><code>-e</code> execute command, in this case, a regex substitution</p> </li> </ul> <h2>After programmatically munging a text file, detect unexpectedly changed lines that don't match certain pattern</h2> <p>If the expected changes will have <code>string1</code> and <code>string2</code> and the unexpected ones should not have them, use <code>diff</code> to find the changesets</p> <pre><code>$ diff file1.txt file2.txt &gt; diff12.txt </code></pre> <p>and find the ones that don't have <code>string1</code> and <code>string2</code></p> <pre> $grep -vE -e '(^.*string2|string2.*$)|^---$|^[[:digit:]]+(,[[:digit:]]+)*c[[:digit:]]+(,[[:digit:]]+)*$' diff12.txt </pre> <p>If you only want to count how many such lines there are, do <code>-vEc</code> instead of <code>-vE</code>.</p> <p>Remarks:</p> <ul> <li> <p><code>-c</code>: count but don't echo the matching lines</p> </li> <li> <p><code>-v</code>: boolean negate the test</p> </li> <li> <p><code>-E</code>: use POSIX extended regular expression</p> </li> <li> <p> <code>^---$|^[[:digit:]]+(,[[:digit:]]+)*c[[:digit:]]+(,[[:digit:]]+)*$</code> are used to match the lines "<code>---</code>" and "<code>5918,5925c5918,5925</code>" that <code>diff</code> generates for <code>diff file1.txt file2.txt</code></p> </li> </ul> <h2>Finding files that do not contain a given string</h2> <p>Find all files:</p> <pre><code>$ find . -type f -name "*.txt" &gt; /tmp/all.txt </code></pre> <p>Find files that contain string <code>foobar</code>:</p> <pre><code>$ ack -l foobar &gt; /tmp/positive.txt </code></pre> <p>Find the complement using <code>grep</code>:</p> <pre><code>$ grep -v -x -f /tmp/positive.txt /tmp/all.txt </code></pre> <p>Remarks:</p> <ul> <li> <p><code>-v</code> invert the matching, selecting non-matching cases;</p> </li> <li> <p><code>-x</code> matches the entire line</p> </li> <li> <p><code>-f &lt;file name&gt;</code> takes patterns from file.</p> </li> </ul> <h2>Extract parts related to the <code>-a</code> option from man page of <code>git</code></h2> <pre><code>git help commit 10 | grep -B 1 -A 10 "\-a" | more </code></pre>