2

I'm having big WordPress SQL dump file on my Ubuntu server which is around 7GB. Since I'm changing domain I need to replace the domain name in the file. I want to know how to do it via command line.

dev.example.com

Needs to replace with

example.com
2
  • 2
    I already tried that and its not working. Its not replacing all the occurrences. May be its breaking due to my string have dot. Nov 3, 2014 at 17:20
  • For all the occurances, add a g: sed 's/text1/text2/g' file-name.
    – muru
    Nov 3, 2014 at 17:27

2 Answers 2

7

you can use sed with inplace editing

sed -i -e 's/dev\.example\.com/example\.com/g' filename
1

I don't know how strict your sed tag is, but a universal way to replace string(s) in a file by the command:

<script> <file> <old_string> <new_string>

you could use the tiny python script below:

#!/usr/bin/env python3

import sys
file = sys.argv[1]; old_string = sys.argv[2]; new_string = sys.argv[3]
with open(file) as src:
    lines = src.read()
print(lines.replace(old_string, new_string))

Copy the script into an empty file, save it as replace.py make it executable (to run it without python3 prefix), run it by the command:

/path/to/replace.py /path/to/file dev.example.com example.com

In case either old_string or new_string has spaces, use quotes:

/path/to/replace.py /path/to/file 'old string with spaces' 'new string with spaces'

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