Sed 脚本仅在一个特定位置在错误位置插入和附加

Sed 脚本仅在一个特定位置在错误位置插入和附加

我正在尝试将手册页转换为 tex,但我不断得到

\item[

在文件的开头和

] \hfill \\

在手册页显示 ENV(1L) 的部分之后。其他一切似乎都在发挥作用。

这是我的 tex.sed:

/^\<[A-Z]*[A-Z]/i \
\\item[

/^\<[A-Z]*[A-Z]/a \
] \\hfill \\\\

1i\
\\documentstyle[11pt]{article} \
\\begin{document}


1i\
\\begin{center} {\\bf 

1a\
\} \\end{center}


2i\
\\begin{description}

$a\
\\end{description}

$a\
\\end{document}


s/\\/\\verb\+\\\+/g
s/%/\\%/g
s/\^/\\\^/g
s/--/-\\hspace\{.01cm\}-/g

s/^+/ \\\\/
s/^-/ \\\\/

这是 env.ascii:

ENV(1L)


NAME
      env - run a program in a modified environment

SYNOPSIS
      env    [-]    [-i]    [-u   name]   [--ignore-environment]
      [--unset=name] [name=value]... [command [args...]]

DESCRIPTION
      This manual page documents the GNU version  of  env.   env
      runs  a  command with an environment modified as specified
      by the command line  arguments.   Arguments  of  the  form
      `variable=value'  set the environment variablevariable to
      valuevalue.  value may be empty (`variable=').  Setting a
      variable to an empty value is different from unsetting it.

      The  first  remaining  argument  specifies  a  program  to
      invoke;  it is searched for according to the specification
      of the PATH environment variable.  Any arguments following
      that are passed as arguments to that program.

      If  no command name is specified following the environment
      specifications,  the  resulting  environment  is  printed.
      This is like specifying a command name of `printenv'.

OPTIONS
     -u, --unset name
             Remove  variable name  from the environment, if it
             was in the environment.

     -, -i, --ignore-environment
             Start  with  an  empty  environment,  ignoring  the
             inherited environment.

      The  long-named options can be introduced with `+' as well
      as `--', for compatibility with previous releases.   
      Eventually  support  for  `+'  will  be removed, because it 
      is incompatible with the POSIX.2 standard.

这就是我的编译方式:

sed -f tex.sed env.ascii > env.tex

这就是我的 env.tex 的样子:

\documentstyle[11pt]{article} 
\begin{document}
\begin{center} {\bf 
\item[
ENV(1L)
] \hfill \\
} \end{center}
\begin{description}


\item[
NAME
] \hfill \\
      env - run a program in a modified environment

\item[
SYNOPSIS
] \hfill \\
      env    [-]    [-i]    [-u   name]   [-\hspace{.01cm}-ignore-environment]
      [-\hspace{.01cm}-unset=name] [name=value]... [command [args...]]

\item[
DESCRIPTION
] \hfill \\
      This manual page documents the GNU version  of  env.   env
      runs  a  command with an environment modified as specified
      by the command line  arguments.   Arguments  of  the  form
      `variable=value'  set the environment variablevariable to
      valuevalue.  value may be empty (`variable=').  Setting a
      variable to an empty value is different from unsetting it.

      The  first  remaining  argument  specifies  a  program  to
      invoke;  it is searched for according to the specification
      of the PATH environment variable.  Any arguments following
      that are passed as arguments to that program.

      If  no command name is specified following the environment
      specifications,  the  resulting  environment  is  printed.
      This is like specifying a command name of `printenv'.

\item[
OPTIONS
] \hfill \\
     -u, --unset name
             Remove  variable name  from the environment, if it
             was in the environment.

     -, -i, --ignore-environment
             Start  with  an  empty  environment,  ignoring  the
             inherited environment.

      The  long-named options can be introduced with `+' as well
      as `-\hspace{.01cm}-', for compatibility with previous releases.   
      Eventually  support  for  `+'  will  be removed, because it 
      is incompatible with the POSIX.2 standard.
\end{description}
\end{document}

笔记

最终结果必须是一个sed能够将手册页正确转换为.tex文件的脚本。请不要用不同的方法来提出解决问题的答案。

答案1

如果您想要的结果只是 PDF 格式的手册页,man它本身就可以为您做到这一点。您将需要该ghostscript工具套件,并且可以执行以下操作,例如获取bash(1)PDF 格式的页面:

man -T ps bash|ps2pdf - bash.pdf

如果您打算使用 LaTeX 生成 DVI 手册页,则不需要ghostscript直接man执行此操作:

man -T dvi bash >bash.dvi

逐字引用 Bichoy 的评论

-T ps 选项对于人类来说不可移植。在 RHEL6 上,正确的语法是 man -t bash,-t 选项使用 /usr/bin/groff -Tps -mandoc 将输出设为 ps

sed关于脚本的一些注意事项

  • 对我有用的是将脚本的前 6 行移到该\\begin{center}行之后。请尝试一下,看看这是否是您想要的结果。
  • 您应该付出相当大的努力来转义手册页内容中的特殊字符。您的脚本目前逃脱了其中一些,但您总有可能忘记其中一个。我可以告诉你至少忘记转义&(La)TeX 用作列分隔符的 & 符号 ( )。这就是为什么 umläute 和我一直建议采用另一种方法来自行 TeXify 整个事情。
  • 您的文档的开头是拼写错误还是实际上应该是\\documentstyle相反的\\documentclass
  • 要排除命令名称被\item标签包围,您应该更改正则表达式以匹配完全由大写字母组成的单词:更改\<[A-Z]*[A-Z]为类似^\s*[A-Z][A-Z]*\s*$.

答案2

你检查过 troff-to-latex 转换器吗tr2乳胶

它可能完全符合您想要做的事情。

相关内容