将包含字符串的段落包含在文本中

时间:2016-08-09 09:27:28

标签: unix awk latex

我有一个很长的参考书目文件被创建用于.tex文件,并想修改它,以便我自己的引用以粗体显示。这可以通过用\ textbf {}封闭包含我的名字(让我们假设它是Crowe)的每个参考书目项目(用空行分隔)来手动实现。

有什么方法可以让这个过程自动化吗?也许用awk?以下是我原始文字的简短示例以及我希望修改的方式(请注意第二个围兜项目前面的 \ textbf {和结束} 最后)。

感谢您的帮助! 罗伯特

%---- ORIGINAL FILE ----%

\begin{thebibliography}{}

\bibitem [\protect \citeauthoryear {%
Aagaard%
\ \BBA {} Andersen%
}{%
Aagaard%
\ \BBA {} Andersen%
}{%
{\protect \APACyear {1998}}%
}]{%
Aagaard:1998aa}
\APACinsertmetastar {%
Aagaard:1998aa}%
\begin{APACrefauthors}%
Aagaard, P.%
\BCBT {}\ \BBA {} Andersen, J\BPBI L.%
\end{APACrefauthors}%
\unskip\
\newblock
\APACrefYearMonthDay{1998}{Aug}{}.
\newblock
{\BBOQ}\APACrefatitle {Correlation between contractile strength and myosin
  heavy chain isoform composition in human skeletal muscle} {Correlation
  between contractile strength and myosin heavy chain isoform composition in
  human skeletal muscle}.{\BBCQ}
\newblock
\APACjournalVolNumPages{Med Sci Sports Exerc}{30}{8}{1217-22}.
\PrintBackRefs{\CurrentBib}

\bibitem [\protect \citeauthoryear {%
Young%
, Stokes%
\BCBL {}\ \BBA {} Crowe%
}{%
Young%
\ \protect \BOthers {.}}{%
{\protect \APACyear {1985}}%
}]{%
Young:1985aa}
\APACinsertmetastar {%
Young:1985aa}%
\begin{APACrefauthors}%
Young, A.%
, Stokes, M.%
\BCBL {}\ \BBA {} Crowe, M.%
\end{APACrefauthors}%
\unskip\
\newblock
\APACrefYearMonthDay{1985}{Apr}{}.
\newblock
{\BBOQ}\APACrefatitle {The size and strength of the quadriceps muscles of old
  and young men} {The size and strength of the quadriceps muscles of old and
  young men}.{\BBCQ}
\newblock
\APACjournalVolNumPages{Clin Physiol}{5}{2}{145-54}.
\PrintBackRefs{\CurrentBib}

\end{the bibliography}

%---- MODIFIED FILE ----%

\begin{thebibliography}{}

\bibitem [\protect \citeauthoryear {%
Aagaard%
\ \BBA {} Andersen%
}{%
Aagaard%
\ \BBA {} Andersen%
}{%
{\protect \APACyear {1998}}%
}]{%
Aagaard:1998aa}
\APACinsertmetastar {%
Aagaard:1998aa}%
\begin{APACrefauthors}%
Aagaard, P.%
\BCBT {}\ \BBA {} Andersen, J\BPBI L.%
\end{APACrefauthors}%
\unskip\
\newblock
\APACrefYearMonthDay{1998}{Aug}{}.
\newblock
{\BBOQ}\APACrefatitle {Correlation between contractile strength and myosin
  heavy chain isoform composition in human skeletal muscle} {Correlation
  between contractile strength and myosin heavy chain isoform composition in
  human skeletal muscle}.{\BBCQ}
\newblock
\APACjournalVolNumPages{Med Sci Sports Exerc}{30}{8}{1217-22}.
\PrintBackRefs{\CurrentBib}

\textbf{\bibitem [\protect \citeauthoryear {%
Young%
, Stokes%
\BCBL {}\ \BBA {} Crowe%
}{%
Young%
\ \protect \BOthers {.}}{%
{\protect \APACyear {1985}}%
}]{%
Young:1985aa}
\APACinsertmetastar {%
Young:1985aa}%
\begin{APACrefauthors}%
Young, A.%
, Stokes, M.%
\BCBL {}\ \BBA {} Crowe, M.%
\end{APACrefauthors}%
\unskip\
\newblock
\APACrefYearMonthDay{1985}{Apr}{}.
\newblock
{\BBOQ}\APACrefatitle {The size and strength of the quadriceps muscles of old
  and young men} {The size and strength of the quadriceps muscles of old and
  young men}.{\BBCQ}
\newblock
\APACjournalVolNumPages{Clin Physiol}{5}{2}{145-54}.
\PrintBackRefs{\CurrentBib}}

\end{the bibliography}

1 个答案:

答案 0 :(得分:0)

awk救援!我认为你有标记问题,但这将做大胆的包装

$ awk -v RS= -v ORS="\n\n" '/bibitem/ && /Crowe/{print "\\textbf{" $0 "}"; next}1' file

如果存在误报,您可以微调模式匹配。

<强>解释

-v RS=设置段落模式,记录由一个或多个空行分隔

ORS="\n\n"使用记录之间的空行标准化输出

/bibitem/ && /Crowe/如果在记录中找到这两种模式......

print "\\textbf{" $0 "}"; next用前缀和后缀引用当前记录($0)并跳到下一条记录

1按原样打印记录