【发布时间】:2020-11-21 21:44:57
【问题描述】:
我有大量 .bib BibTeX 条目保存在一个文件中。我想读取文件,将每篇文章的数据(本质上由@分隔)存储到变量中,提取特定字段,最后将字段(制表符分隔)输出到清理后的文件中。
输入:
@article{Author1_2020,
year = 2020,
month = {feb},
publisher = {Wiley},
...
}
@article{Author2_2010,
year = 2010,
month = {jul},
publisher = {Journal},
...
}
输出:
Wiley 2020 feb
Journal 2010 jul
代码:
while IFS='@' read -r entry; do
p=$(grep "publisher =" <<< "$entry" | cut ...)
y=$(grep "year =" <<< "$entry" | awk ...)
m=$(grep "month =" <<< "$entry" | cut ...)
echo "$p $y $m" >> cleaned_up.bib
done < global.bib
```sh
Is there a way to make the `while read` command in bash operate on delimited chunks of text at a time, instead of single lines? `sed`/`awk` solutions would be more than welcome.
【问题讨论】:
-
IIRC 有一个可用于 python 的 bibtex 解析器,你为什么不使用它呢?用 shell 语言编写解析器将是重新发明轮子。
-
这可能会回答你的问题:[ tex.stackexchange.com ] Command line tool to extract .bib entry by key