대용량 파일의 문자열을 어떻게 바꾸나요?

대용량 파일의 문자열을 어떻게 바꾸나요?

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'

관련 정보