通过 unix shell 命令从乳胶文件中获取参考文献名称的最有效方法是什么?

发布于 2024-09-04 16:07:36 字数 558 浏览 6 评论 0原文

我正在尝试编写一个编译乳胶源的 shell 脚本。我希望获取包含在命令中的参考书目文件的名称,例如:

\bibliography{filename}

我需要将“文件名”保存到 shell 变量中。我对此的解决方案(在 tcsh 中)非常令人尴尬:

set bioliography=grep -v -E "[[:blank:]]*%[[:blank:]]*" poltheory.tex | grep -E "\\参考书目{[A-Za-z0-9_\.]*}" |尾部-1 | sed 's/\\参考书目//' | tr-d {| tr -d } | awk '{print $1}'

这分解为:

  1. 不在乳胶源中显示注释行
  2. 抓取包含有效参考书目标记的那些行
  3. 仅使用最后一个(以防由于某种原因定义了多个
  4. )去掉花括号
  5. 设置 shell 变量剩下的内容。

当然,我忽略了一种优雅的方法来做到这一点。你可以让我惊叹吗?我已经有了一个有效的命令,所以这只是以美丽和 shell 魔法的名义。

I am trying to write a shell script that compiles a latex source. I wish to grab the name of the bibliography file which is contained in a command like:

\bibliography{filename}

I need to save "filename" to a shell variable. My solution to this (in tcsh) is dreadfully embarrassing:

set bioliography=grep -v -E "[[:blank:]]*%[[:blank:]]*" poltheory.tex | grep -E "\\bibliography{[A-Za-z0-9_\.]*}" | tail -1 | sed 's/\\bibliography//' | tr -d { | tr -d } | awk '{print $1}'

This breaks down as:

  1. do not show commented lines in the latex source
  2. grab those lines containing a valid bibliography tag
  3. use only the last one (in case for some reason multiple ones are defined)
  4. get rid of the curly braces
  5. set what is left to the shell variable.

Surely there's an elegant way of doing this that I'm overlooking. Can you wow me? I already have a working command, so this is merely in the name of beauty and shell wizardry.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城烟雨 2024-09-11 16:07:36

规则 1:始终使用 Bourne 系列 shell,而不是 csh 系列 shell 编写 shell 脚本。

从 .aux 文件中提取信息更容易。在这里,我将使用 bash 的一个额外功能来删除文件名末尾的 .tex:

#!/bin/sh
texfile="$1"
auxfile="${texfile%.tex}.aux"

grep '^.bibdata{' "$auxfile" | sed 's/.*{//;s/}.*//'

Rule 1: always write shell scripts using Bourne family shells, not csh family shells.

It's easier to pull the info from the .aux file. Here I'll use an extra feature of bash to chop the .tex off the end of the filename:

#!/bin/sh
texfile="$1"
auxfile="${texfile%.tex}.aux"

grep '^.bibdata{' "$auxfile" | sed 's/.*{//;s/}.*//'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文