UNIX에서 HTML 바이너리 파일의 값 검색 및 바꾸기

UNIX에서 HTML 바이너리 파일의 값 검색 및 바꾸기

내가 만든 HTML 템플릿에서 특정 값을 검색하고 바꾸려고 합니다. 바이너리 파일로서 나는 지금까지 HTML을 검색하고 바꾸는 데 성공하지 못했습니다.

여기서 문자열 1111을 검색하여 1234로 바꿔야 합니다.

style='mso-bookmark:_MailOriginal'><span style='color:#1F497D'>1111</span><o:p></o:p></span></p>

HTML 소스코드에 16진수가 너무 많아서 어떤 명령어를 사용할 수 있는지 추천해주세요.

교체하려는 HTML은 다음과 같습니다.https://pastebin.mozilla.org/8920460

답변1

Python으로 작성된 간단한 스크립트를 사용하여 구현할 수도 있습니다.

.py 교체

f = open("index.html",'r') # open file with read permissions
filedata = f.read() # read contents
f.close() # closes file
filedata = filedata.replace("1111", "1234") # replace 1111 with 1234
filedata = filedata.replace("2222", "2345") # you can add as many replace rules as u need
f = open("index.html",'w') # open the same (or another) file with write permissions
f.write(filedata) # update it replacing the previous strings 
f.close() # closes the file

그런 다음 다음을 실행하십시오.

python replace.py

답변2

샘플 파일test.txt

should not touch 1111
<body>
should touch 1111
</body>
should not touch 1111

사용GNU Awk 3.1.7

awk 'BEGIN {s=0};{if (/<body/) {s=1;} else if (/<\/body>/) {s=0;};if (s) {gsub(1111,1234)}};1' test.txt

결과

should not touch 1111
<body>
should touch 1234
</body>
should not touch 1111

답변3

sed(1) Stream EEditor는 (정규식) 검색 및 교체를 위한 훌륭한 도구입니다.

확인하다 man 1 sed

sed -e s/foo/bar/g infile > outfile

정규식 "foo"와 일치하는 모든 항목은 대체 "bar"로 대체됩니다.

추신. -r교체 부품에서 역참조를 사용해야 하는 경우 이 플래그를 사용하십시오.

관련 정보