Text Processing: Recursive search and replace: Difference between revisions

From WikiMLT
m (Стадий: 4 [Фаза:Авторизиране, Статус:Разработен]; Категория:Linux Server)
m (Стадий: 5 [Фаза:Утвърждаване, Статус:Авторизиран]; Категория:Linux Server)
Line 27: Line 27:
{{devStage  
{{devStage  
  | Прндл  = Linux Server
  | Прндл  = Linux Server
  | Стадий = 4
  | Стадий = 5
  | Фаза  = Авторизиране
  | Фаза  = Утвърждаване
  | Статус = Разработен
  | Статус = Авторизиран
  | ИдтПт  = Spas
  | ИдтПт  = Spas
  | РзбПт  = Spas
  | РзбПт  = Spas
  | АвтПт  = {{REVISIONUSER}}
  | АвтПт  = Spas
  | УтвПт  = Spas
  | УтвПт  = {{REVISIONUSER}}
  | ИдтДт  = 4.08.2022
  | ИдтДт  = 4.08.2022
  | РзбДт  = 4.08.2022
  | РзбДт  = 4.08.2022
  | АвтДт  = {{Today}}
  | АвтДт  = 4.08.2022
  | УтвДт  = 4.08.2022
  | УтвДт  = {{Today}}
  | ИдтРв  = [[Special:Permalink/29901|29901]]
  | ИдтРв  = [[Special:Permalink/29901|29901]]
  | РзбРв  = [[Special:Permalink/29919|29919]]
  | РзбРв  = [[Special:Permalink/29919|29919]]
  | АвтРв  = {{REVISIONID}}
  | АвтРв  = [[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>

Revision as of 12:01, 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 search and 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-matches – 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.

Sim­ple ex­am­ple:

grep -rliZ 'hw\.2022' | xargs -0 sed 's#hw\.2022#homework/hw.2022#g' -i.bak