Text Processing: Recursive search and replace: Difference between revisions
From WikiMLT
m Стадий: 3 [Фаза:Разработване, Статус:Разутвърден]; Категория:Linux Server |
m Стадий: 6 [Фаза:Утвърждаване, Статус:Утвърден]; Категория:Linux Server |
||
(4 intermediate revisions by the same user not shown) | |||
Line 8: | 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 search and replace with Grep, Xargs and Sed == | |||
== Recursive replace with Grep, Xargs and Sed == | |||
<syntaxhighlight lang="shell" line="1">SEARCHED="string or regexp" | <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> | REPLACEMENT="string"</syntaxhighlight>Dry run:<syntaxhighlight lang="shell" line="1">grep -rlZ "$SEARCHED" * | xargs -0 sed 's/${SEARCHED}/${REPLACEMENT}/g'</syntaxhighlight> | ||
Line 16: | Line 15: | ||
<syntaxhighlight lang="shell" line="1">grep -rlZ "$SEARCHED" * | xargs -0 sed 's/${SEARCHED}/${REPLACEMENT}/g' -i.bak</syntaxhighlight> | <syntaxhighlight lang="shell" line="1">grep -rlZ "$SEARCHED" * | xargs -0 sed 's/${SEARCHED}/${REPLACEMENT}/g' -i.bak</syntaxhighlight> | ||
Where: | Where: | ||
* <code class="noTypo">grep</code>: <code class="noTypo">-Z</code>, <code class="noTypo">--null</code> - a data line ends in 0 byte, not newline (null delimiter); | |||
* <code>grep</code>: <code>-Z</code>, <code>--null</code> - a data line ends in 0 byte, not newline (null delimiter); | * <code class="noTypo">grep</code>: <code class="noTypo">-l</code>, <code class="noTypo">--files-with-matches</code> - print only names of FILEs with selected lines; | ||
* <code>grep</code>: <code>-l</code>, <code>--files-with-matches</code> - print only names of FILEs with selected lines; | * <code class="noTypo">xargs</code>: <code class="noTypo">-0</code> <code class="noTypo">--null</code> - items are separated by a null, not white-space; disables quote and backslash processing and logical EOF processing; | ||
* <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 class="noTypo">sed</code>: <code class="noTypo">s</code> - substitute <code class="noTypo">/old/new/</code>, <code class="noTypo">g</code> - all matches to the end of the line; <code class="noTypo">-i.bak</code> do the changes in place and create a backup file. | ||
* <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. | Simple example: | ||
<syntaxhighlight lang="shell" line="1" class="mlw-shell-gray"> | |||
grep -rliZ 'hw\.2022' | xargs -0 sed 's#hw\.2022#homework/hw.2022#g' -i.bak | |||
</syntaxhighlight> | |||
<noinclude> | <noinclude> | ||
<div id='devStage'> | <div id='devStage'> | ||
{{devStage | {{devStage | ||
| Прндл = Linux Server | | Прндл = Linux Server | ||
| Стадий = | | Стадий = 6 | ||
| Фаза = | | Фаза = Утвърждаване | ||
| Статус = | | Статус = Утвърден | ||
| ИдтПт = Spas | | ИдтПт = Spas | ||
| РзбПт = | | РзбПт = Spas | ||
| АвтПт = Spas | | АвтПт = Spas | ||
| УтвПт = | | УтвПт = {{REVISIONUSER}} | ||
| ИдтДт = 4.08.2022 | | ИдтДт = 4.08.2022 | ||
| РзбДт = | | РзбДт = 4.08.2022 | ||
| АвтДт = 4.08.2022 | | АвтДт = 4.08.2022 | ||
| УтвДт = | | УтвДт = {{Today}} | ||
| ИдтРв = [[Special:Permalink/29901|29901]] | | ИдтРв = [[Special:Permalink/29901|29901]] | ||
| РзбРв = | | РзбРв = [[Special:Permalink/29919|29919]] | ||
| АвтРв = | | АвтРв = [[Special:Permalink/29920|29920]] | ||
| РзАРв = [[Special:Permalink/29914|29914]] | | РзАРв = [[Special:Permalink/29914|29914]] | ||
| УтвРв = | | УтвРв = {{REVISIONID}} | ||
| РзУРв = [[Special:Permalink/29916|29916]] | | РзУРв = [[Special:Permalink/29916|29916]] | ||
}} | }} | ||
</div> | </div> | ||
</noinclude> | </noinclude> |
Latest revision as of 11:02, 4 August 2022
Recursive search with Grep
grep -rni 'string or regexp' *
Where:
*
– will match to all files and directories (which doesn't start with.
, alternatively you may need to use./
to match everything within the current directory;-r
,--recursive
– in this case works together with*
(or./
);-n
,--line-number
– output the line number where the match is found,-i
,--ignore-case
– do case insensitive match.
Recursive search and replace 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"
Replace:
grep -rlZ "$SEARCHED" * | xargs -0 sed 's/${SEARCHED}/${REPLACEMENT}/g' -i.bak
Where:
grep
:-Z
,--null
– a data line ends in 0 byte, not newline (null delimiter);grep
:-l
,--files-with-matches
– print only names of FILEs with selected lines;xargs
:-0
--null
– items are separated by a null, not white-space; disables quote and backslash processing and logical EOF processing;sed
:s
– substitute/old/new/
,g
– all matches to the end of the line;-i.bak
do the changes in place and create a backup file.
Simple example:
grep -rliZ 'hw\.2022' | xargs -0 sed 's#hw\.2022#homework/hw.2022#g' -i.bak