sed를 이용해서 문자치환

익명 사용자의 이미지

245
246 DirectoryIndex index.html
247

에서

245
246 DirectoryIndex index.html index.htm index.php
247

으로 문자 치환을 하려하는데 도와주세요 ㅠ

gkgkgk의 이미지

sed '/246/s/\([0-9]*\)[ ]*\(.*\)[ ]*\(index\).html[ ]*/\1 \2 \3.html \3.htm \3.php/'

파이썬3의 이미지

원본 파일은 그대로 둔채 새파일을 만드는 방법을 파이썬으로 만들어봤씁니다.

# -*- coding: utf-8 -*-
 
f = open("data.txt", "r")
data = f.read()
data_new = data.replace("246 DirectoryIndex index.html", "246 DirectoryIndex index.html index.htm index.php")
 
with open("data_new.txt", "w") as nf:
    nf.write(data_new)
 
f.close()
</code>
파이썬3의 이미지

원본 파일을 원본파일.orig 로 백업하고 새파일은 갱신하는 방식으로 다시 썼습니다.

# -*- coding: utf-8 -*-
 
f = open("data.txt", "r")
data = f.read()
data_new = data.replace("246 DirectoryIndex index.html", "246 DirectoryIndex index.html index.htm index.php")
 
with open("data.txt", "w") as nf:
    nf.write(data_new)
 
with open("data.txt.orig", "w") as of:
    of.write(data)
 
f.close()