Differences between revisions 3 and 52 (spanning 49 versions)
Revision 3 as of 2007-08-30 01:43:33
Size: 2901
Editor: GreyCat
Comment: clean-up
Revision 52 as of 2012-07-18 12:36:38
Size: 1878
Editor: Lhunath
Comment: How about a link to ReplacingStrings?
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq21)]]
== How can I replace a string with another string in all files? ==
{{{sed}}} is a good command to replace strings, e.g.
#pragma section-numbers 3
<<Anchor(faq21)>>
== How can I replace a string with another string in a file? ==

We're trying to edit files, which means we need a file editor. Most systems come with a range of editors, but POSIX defines only two that we can use: `ed` and `ex`.

'''Warning''': A lot of bad advice on the Internet will recommend `sed -i` or `perl -i` for this. While on the surface it may look like these tools modify your files, what they ''really'' do is recreate your file and delete the original. This has many downsides and is often very risky. Open handles are destroyed, certain metadata will get lost, a lot of excess disk I/O is involved, and depending on the implementation, interrupted operations may cause your file to get lost. Just use a file editor.

For pure-bash or POSIX-sh solutions or to replace strings in variables or streams, see ReplacingStrings.

`ed` is the standard UNIX command-based editor. `ex` is another POSIX command-based editor with syntax slightly similar to that of `sed`. Here are some commonly-used syntaxes for replacing the string `old` by the string `new` in a file named `file`.
Line 6: Line 14:
    sed 's/olddomain\.com/newdomain.com/g' input > output ex -sc '%s/old/new/ge|x' file
ed -s file <<< $'g/old/s//new/g\nw\nq'
Line 9: Line 18:
To replace a string in all files of the current directory: If you need to edit multiple files, `ex` can easily be combined with `find`. `ed` is hard to call from `find` since it takes commands as input.
Line 12: Line 21:
    for i in *; do
        sed 's/old/new/g' "$i" > atempfile && mv atempfile "$i"
    done
find . -type f -exec ex -sc '%s/old/new/ge|x' {} \;
find . -type f -exec bash -c 'printf "%s\n" "g/old/s//new/g" w q | ed -s "$1"' _ {} \;
Line 17: Line 25:
GNU sed 4.x (but no other version of sed) has a special {{{-i}}} flag which makes the loop and temp file unnecessary: You may notice that this causes an `ex`/`ed` process per file. If your `ex` is provided by `vim` (NOT POSIX), you can optimize that into:
Line 20: Line 28:
      sed -i 's/old/new/g' * find . -type f -exec ex -sc 'argdo %s/old/new/ge|x' {} +
Line 23: Line 31:
Those of you who have perl 5 can accomplish the same thing using this code: This will run a single `ex` for as many files as fit on the command line. You can't do this with `ed`.
Line 25: Line 33:
{{{
    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'
}}}

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 1)
 * 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
----
CategoryShell

How can I replace a string with another string in a file?

We're trying to edit files, which means we need a file editor. Most systems come with a range of editors, but POSIX defines only two that we can use: ed and ex.

Warning: A lot of bad advice on the Internet will recommend sed -i or perl -i for this. While on the surface it may look like these tools modify your files, what they really do is recreate your file and delete the original. This has many downsides and is often very risky. Open handles are destroyed, certain metadata will get lost, a lot of excess disk I/O is involved, and depending on the implementation, interrupted operations may cause your file to get lost. Just use a file editor.

For pure-bash or POSIX-sh solutions or to replace strings in variables or streams, see ReplacingStrings.

ed is the standard UNIX command-based editor. ex is another POSIX command-based editor with syntax slightly similar to that of sed. Here are some commonly-used syntaxes for replacing the string old by the string new in a file named file.

ex -sc '%s/old/new/ge|x' file
ed -s file <<< $'g/old/s//new/g\nw\nq'

If you need to edit multiple files, ex can easily be combined with find. ed is hard to call from find since it takes commands as input.

find . -type f -exec ex -sc '%s/old/new/ge|x' {} \;
find . -type f -exec bash -c 'printf "%s\n" "g/old/s//new/g" w q | ed -s "$1"' _ {} \;

You may notice that this causes an ex/ed process per file. If your ex is provided by vim (NOT POSIX), you can optimize that into:

find . -type f -exec ex -sc 'argdo %s/old/new/ge|x' {} +

This will run a single ex for as many files as fit on the command line. You can't do this with ed.


CategoryShell

BashFAQ/021 (last edited 2022-11-03 23:42:27 by GreyCat)