I have a very long and complex string in many files and I want to remove/replace it recursively. The string contains many slashes, backslashes and spaces and any kind of special signs. How do I do that? A simple find + sed combination wouldn't work because of all the special signs in it which I hardly can escape.

Would it be possible to write the search string to a file and use this as input for a search & replace command?

  • Can the string contain newlines? Commented Jul 18, 2014 at 22:56
  • In my case it's a single line which also starts at the beginning of each file
    – apparat
    Commented Jul 18, 2014 at 22:57
  • What is a "very long" string? Thousands of bytes? Millions? If it's billions, than even a physicist would agree it's very long. And off course any long string can be one line. Like when your particle accelerator experiment is dumping it's raw data in real time. Or a simple program has printed in an infinite loop, and you killed it when you noticed. Commented Oct 24, 2019 at 13:19

2 Answers 2


I assume that the string can contain any character except newlines and null bytes. You can quote the string for use as a sed pattern. The characters $*./[\^ need to be preceded by a backslash. In the replacement text, you need to quote the characters \&/.

regexp=$(printf %s "$old" | sed 's:[$*./\[^]:\\&:g')
replacement=$(printf %s "$new" | sed 's:[\&/]:\\&:g')
sed -e "s/$regexp/$replacement/g"

If you have Perl available, it's simpler.

export old new
perl -pe 's/\Q$ENV{old}/$ENV{new}/'

To act on all the files in the current directory and its subdirectories recursively:

regexp=$(printf %s "$old" | sed 's:[$*./\[^]:\\&:g')
replacement=$(printf %s "$new" | sed 's:[\&/]:\\&:g')
export regexp replacement
find . -type f -exec sh -c 'for x; do sed -e "s/$regexp/$replacement/g" <"$x" >"$x.new" && mv "$x.new" "$x"; done' _ {} +


export old new
find . -type f -exec perl -i -pe 's/\Q$ENV{old}/$ENV{new}/' {} +

Yes, you should be able to use the -f option to specify a file containing a [list of] expression[s]

   -f script-file, --file=script-file

          add the contents of script-file to the commands to be executed

However you will still need to escape any special characters (AFAIK there is no sed equivalent of grep's --fixed-strings) - if perl is available on your system, you may wish to look at using that instead, with the \Q...\E quoted string modifiers.

  • That's not going to help me in this situation because i don't want to escape the hole string. Perl or Python are possible considerations.
    – apparat
    Commented Jul 18, 2014 at 23:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .