
이건 좀 힘든 일이 될 것 같아요.
온라인 문서로 사용하기 위해 html 파일로 추출된 인라인 문서를 조작해야 하는데, 파일의 이러한 부분은
인라인 형식에 html 태그가 없어야 하지만 추출된 html 파일에만 있어야 합니다. 그러나 이러한 문서 부분도 .wiki 파일로 추출되므로 일부 태그는 이미 이와 같습니다.
this is some text describing what is done
<code>
here are
some line that will be shown as code in wiki
but not on html cause they are shown on one line
in html output
</code>
some more describing text
<code>
another piece of code
that shows up as multiple lines in the wiki
but not in htmls
</code>
sed를 통해 쉽게 수행할 수 있는 문서의 이러한 부분을 추출한 후 추출된 파일을 다음 위치에 sed하고 싶습니다.
this is some text describing what is done
<code><br/>
here are <br/>
some line that will be shown as code in wiki <br/>
but not on html cause they are shown on one line<br/>
in html output<br/>
</code><br/>
some more describing text
<code><br/>
another piece of code <br/>
that shows up as multiple lines in the wiki<br/>
but not in htmls<br/>
</code><br/>
지금까지 내가 얻은 것은 다음과 같은 sed 라인입니다.
sed -i '/\<code>/,/\<\/code>/{s/$/\<br\/>/}' file
하지만 다음과 같이 코드 영역 사이의 텍스트에도 html 태그를 추가합니다.
this is some text describing what is done
<code><br/>
here are <br/>
some line that will be shown as code in wiki <br/>
but not on html cause they are shown on one line<br/>
in html output<br/>
</code><br/>
<br/>
some more describing text<br/>
<code><br/>
another piece of code <br/>
that shows up as multiple lines in the wiki<br/>
but not in htmls<br/>
</code><br/>
sed는 첫 번째 태그 사이의 모든 줄에 추가되기 때문에 이것은 기본적으로 정확 and the last
하지만 내가 의도한 것은 아닙니다.
누군가 내가 여기서 놓친 것에 대한 힌트를 줄 수 있습니까?
답변1
백슬래시가 잘못되었습니다. 표현 \<
은~ 아니다리터럴 왼쪽 브로켓과 일치 - 이스케이프되지 않은 <
일치는 그 자체로 잘 일치하지만 백슬래시를 사용하면 슬래시 옆에서 절대 발생할 수 없는 왼쪽 단어 경계 너비가 0인 어설션으로 변경됩니다. 따라서 표현은 \</code>
어떤 것과도 일치할 수 없습니다.
단순히 불필요한 과잉 정확성을 수정하기 위한 약간의 리팩토링을 통해 수정된 sed
스크립트는 다음과 같습니다.
sed -i '/<code>/,\%</code>%s:$:<br/>:' file
백슬래시의 필요성을 더욱 없애기 위해 슬래시를 다른 것으로 자유롭게 변경할 수 있었습니다.
답변2
좋아, 해결책을 찾았습니다. 비록 는 sed
아니지만 awk
.
awk '
BEGIN{c=0} // initialize variable with zero
/\<code>/{c=1} // if start pattern found set variable to 1
{if (c==1) print $0,"<br/>"} // if variable is one, append tag
{if (c==0) print $0} // if variable is zero just print line
/\<\/code>/{c=0} //if endpattern is found set variable to zero
'
이것은 실제로 매우 간단하지만 우아합니다.