Replacing strings in files based on certain search criteria is a very common task. How can I

  • replace string foo with bar in all files in the current directory?
  • do the same recursively for sub directories?
  • replace only if the file name matches another string?
  • replace only if the string is found in a certain context?
  • replace if the string is on a certain line number?
  • replace multiple strings with the same replacement
  • replace multiple strings with different replacements

Replacing all occurrences of one string with another in all files in the current directory:

  • These are for cases where you know that the directory contains only regular files and that you want to process all non-hidden files. If that is not the case, use the another approaches .
  • All sed solutions in this answer assume GNU sed. If using FreeBSD or OS/X, replace i with -i ”. Also note that the use of the -i switch with any version of sed has certain filesystem security implications and is inadvisable in any script which you plan to distribute in any way.

Non recursive, files in this directory only:

[pastacode lang=”javascript” manual=”sed%20-i%20–%20’s%2Ffoo%2Fbar%2Fg’%20*%20%0A%20%20%20%20%20%20%20%20%20%20perl%20-i%20-pe%20’s%2Ffoo%2Fbar%2Fg’%20.%2F*%20%0A” message=”javascript code” highlight=”” provider=”manual”/] [ad type=”banner”]

(the perl one will fail for file names ending in | or space)).

Recursive, regular files (including hidden ones) in this and all subdirectories

[pastacode lang=”javascript” manual=”find%20.%20-type%20f%20-exec%20sed%20-i%20’s%2Ffoo%2Fbar%2Fg’%20%7B%7D%20%2B%0A” message=”javascript code” highlight=”” provider=”manual”/]

If you are using zsh:

[pastacode lang=”javascript” manual=”sed%20-i%20–%20’s%2Ffoo%2Fbar%2Fg’%20**%2F*(D.)%0A” message=”javascript code” highlight=”” provider=”manual”/]

(may fail if the list is too big, see zargs to work around).

Bash can’t check directly for regular files, a loop is needed (braces avoid setting the options globally):

[pastacode lang=”javascript” manual=”(%20shopt%20-s%20globstar%20dotglob%3B%0A%20%20%20%20%20%20%20for%20file%20in%20**%3B%20do%0A%20%20%20%20%20%20%20if%20%5B%5B%20-f%20%24file%20%5D%5D%20%26%26%20%5B%5B%20-w%20%24file%20%5D%5D%3B%20then%20%0A%20%20%20%20%20%20%20%20sed%20-i%20–%20’s%2Ffoo%2Fbar%2Fg’%20%22%24file%E2%80%9C%0A%20%20%20%20%20%20%20%20fi%20%0A%20%20%20%20%20%20%20%20done%20)%0A” message=”javascript code” highlight=”” provider=”manual”/]

The files are selected when they are actual files (-f) and they are writable (-w).

Replace only if the file name matches another string / has a specific extension / is of a certain type etc:

Non-recursive, files in this directory only:

[pastacode lang=”javascript” manual=”sed%20-i%20–%20’s%2Ffoo%2Fbar%2Fg’%20*baz*%20%23%23%20all%20files%20whose%20name%20contains%20baz%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sed%20-i%20–%20’s%2Ffoo%2Fbar%2Fg’%20*.baz%20%23%23%20files%20ending%20in%20.baz%0A%0A” message=”javascript code” highlight=”” provider=”manual”/]

Recursive, regular files in this and all subdirectories

[pastacode lang=”javascript” manual=”find%20.%20-type%20f%20-name%20%22*baz*%22%20-exec%20sed%20-i%20’s%2Ffoo%2Fbar%2Fg’%20%7B%7D%20%2B%0A” message=”javascript code” highlight=”” provider=”manual”/] [pastacode lang=”javascript” manual=”(%20shopt%20-s%20globstar%20dotglob%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sed%20-i%20–%20’s%2Ffoo%2Fbar%2Fg’%20**baz*%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sed%20-i%20–%20’s%2Ffoo%2Fbar%2Fg’%20**.baz%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A” message=”javascript code” highlight=”” provider=”manual”/] [ad type=”banner”]

If you are using zsh:

[pastacode lang=”javascript” manual=”sed%20-i%20–%20’s%2Ffoo%2Fbar%2Fg’%20**%2F*baz*(D.)%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20sed%20-i%20–%20’s%2Ffoo%2Fbar%2Fg’%20**%2F*.baz(D.)%0A” message=”javascript code” highlight=”” provider=”manual”/]

The — serves to tell sed that no more flags will be given in the command line. This is  useful to protect against file names starting with -.

If a file is of a certain type, for example, executable (see man find for more options):

[pastacode lang=”javascript” manual=”f%20ind%20.%20-type%20f%20-executable%20-exec%20sed%20-i%20’s%2Ffoo%2Fbar%2Fg’%20%7B%7D%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20zsh%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sed%20-i%20–%20’s%2Ffoo%2Fbar%2Fg’%20**%2F*(D*)%20%20%20%0A” message=”javascript code” highlight=”” provider=”manual”/]

Replace only if the string is found in a certain context

Replace foo with bar only if there is a baz later on the same line:

[pastacode lang=”javascript” manual=”sed%20-i%20’s%2Ffoo%5C(.*baz%5C)%2Fbar%5C1%2F’%20file%0A” message=”javascript code” highlight=”” provider=”manual”/]

In sed, using \( \) saves whatever is in the parentheses and you can then access it with \1. There are many  variations of this theme, to learn more about such regular expressions,

Replace foo with bar only if foo is found on the 3d column (field) of the input file (assuming whitespace-separated fields):

[pastacode lang=”javascript” manual=”gawk%20-i%20inplace%20’%7Bgsub(%2Ffoo%2F%2C%22baz%22%2C%243)%3B%20print%7D’%20file(needs%C2%A0gawk%C2%A04.1.0%20or%20newer).%0A%20%0A” message=”javascript code” highlight=”” provider=”manual”/]

For a different field just use $N where N is the number of the field of interest.

For a different field separator (: in this example) use

 

[pastacode lang=”javascript” manual=”gawk%20-i%20inplace%20-F’%3A’%20’%7Bgsub(%2Ffoo%2F%2C%22baz%22%2C%243)%3Bprint%7D’%20file%0A” message=”javascript code” highlight=”” provider=”manual”/]

Another solution using perl:

 

[pastacode lang=”javascript” manual=”perl%20-i%20-ane%20’%24F%5B2%5D%3D~s%2Ffoo%2Fbaz%2Fg%3B%20%24%22%20%3D%20%22%20%22%3B%20print%20%22%40F%5Cn%22’%20foo%20″ message=”javascript code” highlight=”” provider=”manual”/]

Both the awk and perl solutions will affect spacing in the file (remove the leading and trailing blanks, and convert sequences of blanks to one space character in those lines that match). For a different field, use $F[N-1] where N is the field number you want and for a different field separator use (the $”=”:” sets the output field separator to :):

         

[pastacode lang=”javascript” manual=”%20%20%20%20%20%20%20%20%20%20%20%20%20%20perl%20-i%20-F’%3A’%20-ane%20’%24F%5B2%5D%3D~s%2Ffoo%2Fbaz%2Fg%3B%20%24%22%3D%22%3A%22%3Bprint%20%22%40F%22’%20foo%20%0A” message=”javascript code” highlight=”” provider=”manual”/]

Replace foo with bar only on the 4th line:

[pastacode lang=”javascript” manual=”sed%20-i%20’4s%2Ffoo%2Fbar%2Fg’%20file%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20gawk%20-i%20inplace%20’NR%3D%3D4%7Bgsub(%2Ffoo%2F%2C%22baz%22)%7D%3B1’%20file%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20perl%20-i%20-pe%20’s%2Ffoo%2Fbar%2Fgif%20%20%20%24.%3D%3D4’%20file%20%20%0A” message=”javascript code” highlight=”” provider=”manual”/]

Multiple replace operations: replace with different strings

You can combine sed commands:

[pastacode lang=”javascript” manual=”sed%20-i%20’s%2Ffoo%2Fbar%2Fg%3B%20s%2Fbaz%2Fzab%2Fg%3B%20s%2FAlice%2FJoan%2Fg’%20file” message=”javascript code” highlight=”” provider=”manual”/] [ad type=”banner”]

Be aware that order matters (sed ‘s/foo/bar/g; s/bar/baz/g‘ will substitute foo with baz).

Perl commands:

[pastacode lang=”javascript” manual=”perl%20-i%20-pe%20’s%2Ffoo%2Fbar%2Fg%3B%20s%2Fbaz%2Fzab%2Fg%3B%20s%2FAlice%2FJoan%2Fg’%20file%0A” message=”javascript code” highlight=”” provider=”manual”/]

If you have a large number of patterns, it is easier to save your patterns and their replacements in a sed script file:

[pastacode lang=”javascript” manual=”%23!%20%2Fusr%2Fbin%2Fsed%20-f%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20s%2Ffoo%2Fbar%2Fg%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20s%2Fbaz%2Fzab%2Fg%0A” message=”javascript code” highlight=”” provider=”manual”/]

If you have too many pattern pairs for the above to be feasible, you can read pattern pairs from a file (two space separated patterns, $pattern and $replacement, per line):

[pastacode lang=”javascript” manual=”while%20read%20-r%20pattern%20replacement%3B%20do%20%0A%20sed%20-i%20%22s%2F%24pattern%2F%24replacement%2F%22%20file%20%0A%20done%20%3C%20patterns.txt%0A” message=”javascript code” highlight=”” provider=”manual”/]

That will be quite slow for long lists of patterns and large data files so you might want to read the patterns and create a sed script from them instead.   The  following assumes a <space> delimiter separates a list of MATCH<space>REPLACE pairs occurring one-per-line in the file patterns.txt :

[pastacode lang=”javascript” manual=”%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sed%20’s%7C%20*%5C(%5B%5E%20%5D*%5C)%20*%5C(%5B%5E%20%5D*%5C).*%7Cs%2F%5C1%2F%5C2%2Fg%7C’%20%3Cpatterns.txt%20%7C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sed%20-f-%20.%2Feditfile%20%3Eoutfile%0A” message=”javascript code” highlight=”” provider=”manual”/]
  • The above format is largely arbitrary and, for example, doesn’t allow for a <space> in either of MATCH or REPLACE.
  •  if  you can create an output stream which looks like a sed script, then you can source that stream as a sed script by specifying sed’s script file as –stdin.
  • You can combine and concatenate multiple scripts in similar fashion:
[pastacode lang=”javascript” manual=”SOME_PIPELINE%20%7C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sed%20-e’%23some%20expression%20script’%20%5C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-f.%2Fscript_file%20-f-%20%5C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-e%E2%80%98%23more%20%20inline%20expressions’%20%5C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.%2Factual_edit_file%20%3E.%2Foutfile%0A” message=”javascript code” highlight=”” provider=”manual”/]
  •  A POSIX sed will concatenate all scripts into one in the order they appear on the command-line. None of these need end in a \newline.
  • grep can work the same way:
[pastacode lang=”javascript” manual=”%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sed%20-e’%23generate%20a%20pattern%20list’%20%3Cin%20%7C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20grep%20-f-%20.%2Fgrepped_file%0A” message=”javascript code” highlight=”” provider=”manual”/]
  • When working with fixed-strings as patterns, it is good practice to escape regular expression metacharacters. You can do this rather easily:
[pastacode lang=”javascript” manual=”sed%20’s%2F%5B%5D%24%26%5E*%5C.%2F%5B%5D%2F%5C%5C%26%2Fg%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20s%7C%20*%5C(%5B%5E%20%5D*%5C)%20*%5C(%5B%5E%20%5D*%5C).*%7Cs%2F%5C1%2F%5C2%2Fg%7C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20’%20%20%3Cpatterns.txt%20%7C%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sed%20-f-%20.%2Feditfile%20%3Eoutfile%20%20%0A” message=”javascript code” highlight=”” provider=”manual”/]

Multiple replace operations: replace multiple patterns with the same string

Replace any of foo, bar or baz with foobar

[pastacode lang=”javascript” manual=”sed%20-Ei%20’s%2Ffoo%7Cbar%7Cbaz%2Ffoobar%2Fg’%20file%0A” message=”javascript code” highlight=”” provider=”manual”/]

or  using perl

[pastacode lang=”javascript” manual=”perl%20-i%20-pe%20’s%2Ffoo%7Cbar%7Cbaz%2Ffoobar%2Fg’%20file%0A” message=”javascript code” highlight=”” provider=”manual”/]

  • If you have list of files you can use,
[pastacode lang=”javascript” manual=”replace%20%22old_string%22%20%22new_string%22%20–%20file_name1%20file_name2%20file_name3%20%0A” message=”javascript code” highlight=”” provider=”manual”/]

Categorized in: