Awk 追加文本块的副本

Awk 追加文本块的副本,awk,vim,sed,Awk,Vim,Sed,我正在制作一个LaTeXbeamer演示文稿,我想在每个\item标题下添加一个注释部分,该部分只复制其文本。比如我有 \begin{frame}{Frame Title} \begin{itemize}[<+->] \item Einstein was a clever man. \item My favorite equation is, \begin{equation} E = m c^2. \end{equation} wher

我正在制作一个
LaTeX
beamer
演示文稿,我想在每个
\item
标题下添加一个注释部分,该部分只复制其文本。比如我有

\begin{frame}{Frame Title}
\begin{itemize}[<+->]
    \item Einstein was a clever man.
    \item My favorite equation is,
    \begin{equation}
        E = m c^2.
    \end{equation} where
    \begin{enumerate}
        \item $m$ is the mass,
        \item $c$ the velocity of light and,
        \item $E$ is the energy.
    \end{enumerate}
\end{itemize}
\end{frame}
但是我无法捕获属于每个顶级
\item

的整个块这可能对您有用(GNU-sed):

sed-E'/^\\begin\{itemize\}/{:a;n;/^\\end\{itemize\}/bb
/^({4}|\t)\\项目/{
:b;x;s/^(\s*)\\item(.*)/\1\\note[item]{\n\1\1\2\n\1}/p;x;h;ba}
H、 ba}文件
关注
\begin{itemize}
\end{itemize}

制作每个
\item
的副本,并将副本附加到每个
\item
节,用所需的
\note
命令替换
\item
命令


注意:解决方案取决于
\item
命令中的空格缩进,目前缩进设置为4个空格,但可以使用制表符(请参阅第二行开头的regexp)。

请在您的问题中添加您的努力,我们强烈鼓励您这样做,谢谢。预期结果与原始状态之间的差异与以下问题不一致:a)未提及调整缩进或应嵌套或不应嵌套的内容;b)要求“在每个
\item
”下发生某些事情。感谢您指出@romainl。我已经修复了缩进,并且我希望每个顶级
\item
列表都在它们下面重复,但是在
\note[item]{…}
块中。先生,您是个天才@Pottong快速跟进。如果
\begin{itemize}[]的\end{itemize}
块向前缩进一个制表符/4个空格--表达式将如何更改?@然后更改解决方案第一行中的regexp,即
/^({4}|\t)\\begin\{itemize\}/{:a;n;/^({4}|\t)\\end\{itemize}/bb
\begin{frame}{Frame Title}
\begin{itemize}[<+->]
    \item Einstein was a clever man.
    \note<.>[item]{
          Einstein was a clever man.
    }
    \item My favorite equation is,
    \begin{equation}
        E = m c^2.
    \end{equation} where
    \begin{enumerate}
        \item $m$ is the mass,
        \item $c$ the velocity of light and,
        \item $E$ is the energy.
    \end{enumerate}

    \note<.>[item] { 
          My favorite equation is,
    \begin{equation}
        E = m c^2.
    \end{equation where
    \begin{enumerate}
        \item $m$ is the mass,
        \item $c$ the velocity of light and,
        \item $E$ is the energy.
    \end{enumerate}
    }
\end{itemize}
\end{frame}
:g/^\\item/ copy . | s//\\note\{item\}<.>\{/g
sed -E '/^\\begin\{itemize\}/{:a;n;/^\\end\{itemize\}/bb
        /^( {4}|\t)\\item/{
          :b;x;s/^(\s*)\\item(.*)/\1\\note<.*>[item]{\n\1\1\2\n\1}/p;x;h;ba}
        H;ba}' file