我的 Ubuntu 伺服器上有大約 7GB 的大型 WordPress SQL 轉儲檔案。由於我要更改域名,我需要替換文件中的域名。我想知道如何透過命令列執行此操作。
dev.example.com
需要更換為
example.com
答案1
您可以使用 sed 進行就地編輯
sed -i -e 's/dev\.example\.com/example\.com/g' filename
答案2
我不知道你的sed
標籤有多嚴格,但透過命令替換檔案中的字串的通用方法:
<script> <file> <old_string> <new_string>
你可以使用下面的小Python腳本:
#!/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))
將腳本複製到一個空文件中,將其另存為replace.py
使其可執行(以不帶前綴的方式運行它python3
),並通過以下命令運行它:
/path/to/replace.py /path/to/file dev.example.com example.com
如果 old_string 或 new_string 有空格,請使用引號:
/path/to/replace.py /path/to/file 'old string with spaces' 'new string with spaces'