Text Processing: Recursive search and replace: Difference between revisions

From WikiMLT
mNo edit summary
mNo edit summary
Line 1: Line 1:
<noinclude>{{ContentArticleHeader/Linux_Server}}</noinclude>
<noinclude>{{ContentArticleHeader/Linux_Server}}</noinclude>
== Recursive search with Grep ==
<syntaxhighlight lang="shell" line="1">grep -rni 'string or regexp' *</syntaxhighlight>
<syntaxhighlight lang="shell" line="1">grep -rni 'string or regexp' *</syntaxhighlight>
Where:
Where:
* <code class="noTypo">*</code> - will match to all files and directories (which doesn't start with <code class="noTypo">.</code>, alternatively you may need to use <code class="noTypo">./</code> to match everything within the current directory;
* <code class="noTypo">*</code> - will match to all files and directories (which doesn't start with <code class="noTypo">.</code>, alternatively you may need to use <code class="noTypo">./</code> to match everything within the current directory;
Line 7: Line 8:
* <code class="noTypo">-n</code>, <code class="noTypo">--line-number</code> - output the line number where the match is found,
* <code class="noTypo">-n</code>, <code class="noTypo">--line-number</code> - output the line number where the match is found,
* <code class="noTypo">-i</code>, <code class="noTypo">--ignore-case</code> - do case insensitive match.
* <code class="noTypo">-i</code>, <code class="noTypo">--ignore-case</code> - do case insensitive match.
== Recursive replace with Grep, Xargs and Sed ==
<syntaxhighlight lang="shell" line="1">SEARCHED="string or regexp"
REPLACEMENT="string"</syntaxhighlight>Dry run:<syntaxhighlight lang="shell" line="1">grep -rlZ "$SEARCHED" * | xargs -0 sed 's/${SEARCHED}/${REPLACEMENT}/g'</syntaxhighlight>
<syntaxhighlight lang="shell" line="1">grep -rlZ "$SEARCHED" * | xargs -0 sed 's/${SEARCHED}/${REPLACEMENT}/g' | grep "$REPLACEMENT"</syntaxhighlight>
Replace:
<syntaxhighlight lang="shell" line="1">grep -rlZ "$SEARCHED" * | xargs -0 sed 's/${SEARCHED}/${REPLACEMENT}/g' -i.bak</syntaxhighlight>
Where:
* <code>grep</code>: <code>-Z</code>, <code>--null</code> - a data line ends in 0 byte, not newline (null delimiter);
* <code>grep</code>: <code>-l</code>, <code>--files-with-matches</code> - print only names of FILEs with selected lines;
* <code>xargs</code>: <code>-0</code> <code>--null</code> - items are separated by a null, not white-space; disables quote and backslash processing and logical EOF processing;
* <code>sed</code>: <code>s</code> - substitute <code>/old/new/</code>, <code>g</code> - all matches to the end of the line; <code>-i.bak</code> do the changes in place and create a backup file.


<noinclude>
<noinclude>
<div id='devStage'>
<div id="devStage">
{{devStage  
{{devStage  
  | Прндл  = Linux Server
  | Прндл  = Linux Server

Revision as of 11:56, 4 August 2022

Re­cur­sive search with Grep

grep -rni 'string or regexp' *

Where:

  • * – will match to all files and di­rec­to­ries (which doesn't start with ., al­ter­na­tive­ly you may need to use ./ to match every­thing with­in the cur­rent di­rec­to­ry;
  • -r, --recursive – in this case works to­geth­er with * (or ./);
  • -n, --line-number – out­put the line num­ber where the match is found,
  • -i, --ignore-case – do case in­sen­si­tive match.

Re­cur­sive re­place with Grep, Xargs and Sed

SEARCHED="string or regexp"
REPLACEMENT="string"

Dry run:

grep -rlZ "$SEARCHED" * | xargs -0 sed 's/${SEARCHED}/${REPLACEMENT}/g'
grep -rlZ "$SEARCHED" * | xargs -0 sed 's/${SEARCHED}/${REPLACEMENT}/g' | grep "$REPLACEMENT"

Re­place:

grep -rlZ "$SEARCHED" * | xargs -0 sed 's/${SEARCHED}/${REPLACEMENT}/g' -i.bak

Where:

  • grep: -Z, –null – a da­ta line ends in 0 byte, not new­line (null de­lim­iter);
  • grep: -l, –files-with-match­es – print on­ly names of FILEs with se­lect­ed lines;
  • xargs: -0 –null – items are sep­a­rat­ed by a null, not white-space; dis­ables quote and back­slash pro­cess­ing and log­i­cal EOF pro­cess­ing;
  • sed: s – sub­sti­tute /​​​old/​​​new/​​​, g – all match­es to the end of the line; -i.bak do the changes in place and cre­ate a back­up file.