Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
docs:bash:search_and_replace_in_files [2009/05/29 12:00] billhdocs:bash:search_and_replace_in_files [2009/06/03 10:45] (current) billh
Line 3: Line 3:
 grep -Z -R -l -E 'searchpattern' . | xargs -0 sed -i 's/searchpattern/replacestring/g' grep -Z -R -l -E 'searchpattern' . | xargs -0 sed -i 's/searchpattern/replacestring/g'
 </code> </code>
 +
 +===== Advanced Example =====
 +  * :!: remember to escape parenthesis when using [[sed]]
 +  * this example finds all occurrences of 'oldhostname' where it is not followed by a period, and replaces it with 'newhostname' 
 +<code>
 +grep -Z -R -l -E 'oldhostname[^\.]' . | xargs -0 sed -i 's/oldhostname\([^\.]\)/newhostname\1/g'
 +</code>
 +
 +===== FreeBSD 5 Example =====
 +  * the -Z parameter was different for grep, so it actually wanted %%--null%%
 +  * the backup extension for sed was not optional, so '.sed' was used
 +<code>
 +grep --null -R -l -E 'oldhostname[^\.]' . | xargs -0 -t sed -i '.sed' 's/newhostname\([^\.]\)/xdev\1/g'
 +</code>
 +
 +===== See Also =====
 +  * [[sed]]
 +
  • docs/bash/search_and_replace_in_files.1243620010.txt.gz
  • Last modified: 2009/05/29 12:00
  • by billh