
다음 줄이 포함된 파일이 있습니다 LaTeX
.foo.tex
\includegraphics[width=0.49\textwidth]{img1}
\includegraphics{img2}
\subfloat{\includegraphics[width=0.3\textwidth]{img3}}
\subfloat{\includegraphics{img4}}
파일 확장자를 추가하기 위해 파일에서 다음 줄을 바꾸고 싶습니다.
\includegraphics[width=0.49\textwidth]{img1.png}
\includegraphics{img2.png}
\subfloat{\includegraphics[width=0.3\textwidth]{img3.png}}
\subfloat{\includegraphics{img4.png}}
나는 다음 명령을 시도하여 시작했습니다 sed
.
cat foo.tex | sed 's/\(includegraphics.*[a-z,0-9,\-]}\)/\1.png}/g' > bar.tex
하지만 다음과 같이 반환됩니다.
\includegraphics[width=0.49\textwidth]{img1}.png
\includegraphics{img2}.png
\subfloat{\includegraphics[width=0.3\textwidth]{img3}.png}
\subfloat{\includegraphics{img4}.png}
sed
파일 확장자를 추가하는 명령을 수정하는 방법은 무엇입니까 ?
편집하다
이미지 파일 이름은 img1, img2 등이 아닙니다.
답변1
$ sed '/\\includegraphics/s/img[0-9]*/&.png/g' input
\includegraphics[width=0.49\textwidth]{img1.png}
\includegraphics{img2.png}
\subfloat{\includegraphics[width=0.3\textwidth]{img3.png}}
\subfloat{\includegraphics{img4.png}}
일치 항목 끝에 img
추가되는 임의의 숫자(0 숫자 포함)가 뒤따르는 대체 작업에 대한 정규식 검색입니다 ..png
교체만 수행하는 약간 더 강력한 버전~에큰 괄호:
$ sed '/\\includegraphics/s/{\(img[0-9]*\)}/{\1.png}/g' input
\includegraphics[width=0.49\textwidth]{img1.png}
\includegraphics{img2.png}
\subfloat{\includegraphics[width=0.3\textwidth]{img3.png}}
\subfloat{\includegraphics{img4.png}}
{img3}
이 명령은 (예를 들어) 가 포함된 줄을 찾아서 \includegraphics
추출 하고 img3
추가 .png
한 다음 새 중괄호로 다시 감싸서 원하는 {img3.png}
.