pages tagged ack http://meng6net.localhost/tag/ack/ <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 ack http://meng6net.localhost/computing/example/examples_of_using_ack/ http://meng6net.localhost/computing/example/examples_of_using_ack/ ack computing example Tue, 16 May 2017 23:59:39 +0000 2017-05-16T23:59:39Z <h2>Find instances of <code>ToDo</code> in source files</h2> <pre><code>ack -r Client . </code></pre> <h2>Find number of files where string <code>ToDo</code> occurs</h2> <pre><code>ack -l 'ToDo' sourceDirectory | wc -l 12 </code></pre> <h2>Find string <code>"FooBar"</code> in any <code>.*</code> files ignoring certain directories</h2> <pre><code>$ pwd /User/lumeng $ ack --type=matlab --ignore-dir={subdir1,subdir2} "\"FooBar\"" </code></pre> <p>Remarks:</p> <ul> <li><code>/User/lumeng/subdir1</code>, <code>/User/lumeng/subdir2</code> are not searched;</li> <li><code>--type=matlab</code> specifies that files with file name extension <code>.m</code> is searched; Check <code>ack --help=type</code> for complete list of types;</li> </ul> <h2>Find <code>Plot</code> in Mathematica and related source code files</h2> <pre> <code>ack --type-add=wolfram:ext:.m,.nb,.cdf,.mt,.wl,.wlt --wolfram Plot </code></pre> <p>Optionally, add <code>--type-add=wolfram:ext:.m,.nb,.cdf,.mt,.wl,.wlt</code> to a <code>${HOME}/.ackrc</code> and ue</p> <pre><code>ack --wolfram Plot ack --nowolfram Plot </code></pre> 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>