3348
Comment:
|
3429
./* and -- fixes
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
[[Anchor(faq21)]] | <<Anchor(faq21)>> |
Line 8: | Line 8: |
'''Note that input and output must differ.''' | |
Line 12: | Line 13: |
for i in *; do | for i in ./*; do |
Line 20: | Line 21: |
sed -i 's/old/new/g' * | sed -i 's/old/new/g' ./* |
Line 26: | Line 27: |
sed -i '' 's/old/new/g' * | sed -i '' 's/old/new/g' ./* |
Line 34: | Line 35: |
perl -pi -e 's/old/new/g' * | perl -pi -e 's/old/new/g' ./* |
Line 46: | Line 47: |
perl -ni -e 'print unless /foo/' * | perl -ni -e 'print unless /foo/' ./* |
Line 68: | Line 69: |
[ $# -lt 1 ] && set -- * | [ $# -lt 1 ] && set -- ./* |
Line 75: | Line 76: |
sed "s|$old|$new|g" "$file" > "$file"-new || exit | sed "s|$old|$new|g" -- "$file" > "$file"-new || exit |
Line 77: | Line 78: |
if cmp "$file" "$file"-new >/dev/null 2>&1 then rm "$file"-new # file has not changed else mv "$file"-new "$file" # file has changed: overwrite original file |
if cmp -- "$file" "$file"-new >/dev/null 2>&1 then rm -- "$file"-new # file has not changed else mv -- "$file"-new "$file" # file has changed: overwrite original file |
Line 91: | Line 92: |
* use another {{{sed}}} separator character than '|', e.g. ^A (ASCII 1) * the [:UsingFind:find] command above could use either {{{xargs}}} or the built-in {{{xargs}}} of POSIX find |
* use another {{{sed}}} separator character than '|', e.g. ^A (ASCII 0x01) * the [[UsingFind|find]] command above could use either {{{xargs}}} or the built-in {{{xargs}}} of POSIX find |
Line 94: | Line 95: |
Note: {{{set -- *}}} in the code above is safe with respect to files whose names contain spaces. The expansion of * by {{{set}}} is the same as the expansion done by {{{for}}}, and filenames will be preserved properly as individual parameters, and not broken into words on whitespace. | Note: {{{set -- ./*}}} in the code above is safe with respect to files whose names contain spaces. The expansion of `./*` by {{{set}}} is the same as the expansion done by {{{for}}}, and filenames will be preserved properly as individual parameters, and not broken into words on whitespace. |
How can I replace a string with another string in all files?
sed is a good command to replace strings, e.g.
sed 's/olddomain\.com/newdomain.com/g' input > output
Note that input and output must differ.
To replace a string in all files of the current directory:
for i in ./*; do sed 's/old/new/g' "$i" > atempfile && mv atempfile "$i" done
GNU sed 4.x has a special -i flag which makes the loop and temp file unnecessary:
sed -i 's/old/new/g' ./*
On some (but not all) BSD systems, sed has a -i flag as well, but it takes a mandatory argument. The above example then becomes
sed -i '' 's/old/new/g' ./*
which in turn does not work with GNU sed. Effectively, whenever portability matters, sed -i should be avoided.
Those of you who have perl 5 can accomplish the same thing using this code:
perl -pi -e 's/old/new/g' ./*
Recursively (requires GNU or BSD find):
find . -type f -print0 | xargs -0 perl -pi -e 's/old/new/g'
If you want to delete lines instead of making substitutions:
perl -ni -e 'print unless /foo/' ./* # Deletes any line containing the perl regex foo
To replace for example all "unsigned" with "unsigned long", if it is not "unsigned int" or "unsigned long" ...:
find . -type f -print0 | xargs -0 perl -i.bak -pne \ 's/\bunsigned\b(?!\s+(int|short|long|char))/unsigned long/g'
Finally, for those of you with none of the useful things above, here's a script that may be useful:
#!/bin/sh # chtext - change text in several files # neither string may contain '|' unquoted old='olddomain\.com' new='newdomain\.com' # if no files were specified on the command line, use all files: [ $# -lt 1 ] && set -- ./* for file do [ -f "$file" ] || continue # do not process e.g. directories [ -r "$file" ] || continue # cannot read file - ignore it # Replace string, write output to temporary file. Terminate script in case of errors sed "s|$old|$new|g" -- "$file" > "$file"-new || exit # If the file has changed, overwrite original file. Otherwise remove copy if cmp -- "$file" "$file"-new >/dev/null 2>&1 then rm -- "$file"-new # file has not changed else mv -- "$file"-new "$file" # file has changed: overwrite original file fi done
If the code above is put into a script file (e.g. chtext), the resulting script can be used to change a text e.g. in all HTML files of the current and all subdirectories:
find . -type f -name '*.html' -exec chtext {} \;
Many optimizations are possible:
use another sed separator character than '|', e.g. ^A (ASCII 0x01)
the find command above could use either xargs or the built-in xargs of POSIX find
Note: set -- ./* in the code above is safe with respect to files whose names contain spaces. The expansion of ./* by set is the same as the expansion done by for, and filenames will be preserved properly as individual parameters, and not broken into words on whitespace.
A more sophisticated example of chtext is here: http://www.shelldorado.com/scripts/cmds/chtext