대용량 HTML 문서 편집을 위한 Bash 스크립트

대용량 HTML 문서 편집을 위한 Bash 스크립트

많은 HTML 문서가 포함된 디렉토리가 있습니다. 대부분은 코드 블록을 포함합니다.

      .org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
        text-decoration: underline;
      }

라벨 내에서 <style type="text/css">. text-decoration: underline;각 파일의 해당 블록에 있는 줄을 삭제하는 스크립트를 작성하고 싶습니다 .

일반적으로 나는 해당 줄의 모든 인스턴스를 삭제하기 위해 한 줄 sed또는 여러 줄을 작성 하지만 많은 문서에는 삭제하지 않은 해당 줄의 다른 인스턴스가 있습니다.perltext-decoration: underline;text-decoration: underline

이 작업을 쉽게 수행할 수 있는 도구가 Linux에 있습니까?

답변1

이 시도:

sed '/.org-link {/,/}/{/text-decoration: underline;/d}' file

산출:

      .org-링크{
        /*조직 링크*/
        색상: #b58900;
        글꼴 두께: 굵게;
      }

"제자리에서" 파일을 편집하려면:

sed -i '/.org-link {/,/}/{/text-decoration: underline;/d}' file

답변2

사용 gawk:

gawk -i inplace '/.org-link {/,/}/ {if($0~/text-decoration: underline/) next} {print}' infile

text-decoration: underline이렇게 하면 클래스 의 속성 만 제거됩니다 .org-link.

user@debian ~ % cat infile
.org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
        text-decoration: underline;
      }

.org-link1 {
        /* org-link */
        color: #b58900;
        font-weight: bold;
        text-decoration: underline;
      }

.org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
        text-decoration: underline;
      }
user@debian ~ % gawk -i inplace '/.org-link {/,/}/ {if($0~/text-decoration: underline/) next} {print}' infile
user@debian ~ % cat infile
.org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
      }

.org-link1 {
        /* org-link */
        color: #b58900;
        font-weight: bold;
        text-decoration: underline;
      }

.org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
      }

동일한 작업 디렉터리에 있는 여러 HTML 파일을 반복하려면 bash for와일드카드가 포함된 루프를 사용할 수 있습니다.

for f in *.html; do gawk -i inplace '/.org-link {/,/}/ {if($0~/text-decoration: underline/) next} {print}' "$f"; done

관련 정보