使用正则表达式重新格式化 BibTeX 文件中的参考标题

使用正则表达式重新格式化 BibTeX 文件中的参考标题

我在 BibTeX 文件中有很多参考文献,如下所示:

@inproceedings{lechartier2014static,
  title={Static and Dynamic Modeling of a PEMFC for Prognostics Purpose},
  author={Lechartier, Elodie and Gouriveau, Rafael and Pera, Marie-Cecile and Hissel, Daniel and Zerhouni, Noureddine},
  booktitle={Vehicle Power and Propulsion Conference (VPPC), 2014 IEEE},
  pages={1--5},
  year={2014},
  organization={IEEE}
}
@inproceedings{zein2013statistical,
  title={Statistical approach to online prognostics of turbine engine components},
  author={Zein-Sabatto, Saleh and Bodruzzaman, Jabir and Mikhail, Mervat},
  booktitle={Southeastcon, 2013 Proceedings of IEEE},
  pages={1--6},
  year={2013},
  organization={IEEE}
}

原始文件链接(每个条目前面的空格数title=有所不同)。

如何使用正则表达式将所有标题部分更改为:

  title={\textbf{Static and Dynamic Modeling of a PEMFC for Prognostics Purpose}},

答案1

这很好用:

perl -i.bak -pe 's|^(\s*title=)(.+?)(,)$|$1\{\\textbf$2\}$3|' ProgRef.bib

这会重新格式化所有标题部分,并假设每行只有一个标题。它还会备份您的文件(带扩展名.bak)。

更新:如果您的输入文件有 DOS/Windows 行尾,请执行以下操作:

  1. sudo apt-get install dos2unix
  2. dos2unix ProgRef.bib
  3. 运行上述正则表达式

答案2

使用sed

sed 's/^\( *\)title=\(.*\),/\1title={\\textbf\2},/' in

要就地编辑文件而不是将编辑后的文件打印到stdout,请添加以下-i选项:

sed -i 's/^\( *\)title=\(.*\),/\1title={\\textbf\2},/' in
  • \( *\): 匹配并分组行首的任意数量的字符;
  • title=: 匹配一个^title=字符串;
  • \(.*\),:贪婪地匹配并将任意数量的任意字符分组在一个,字符和一个,字符之前;
  • title={\\textbf\1},:用一个字符串替换匹配项,title={\textbf该字符串后跟捕获的组,后跟一个},字符串;
% cat in
@inproceedings{lechartier2014static,
  title={Static and Dynamic Modeling of a PEMFC for Prognostics Purpose},
  author={Lechartier, Elodie and Gouriveau, Rafael and Pera, Marie-Cecile and Hissel, Daniel and Zerhouni, Noureddine},
  booktitle={Vehicle Power and Propulsion Conference (VPPC), 2014 IEEE},
  pages={1--5},
  year={2014},
  organization={IEEE}
}
@inproceedings{zein2013statistical,
  title={Statistical approach to online prognostics of turbine engine components},
  author={Zein-Sabatto, Saleh and Bodruzzaman, Jabir and Mikhail, Mervat},
  booktitle={Southeastcon, 2013 Proceedings of IEEE},
  pages={1--6},
  year={2013},
  organization={IEEE}
}
% sed 's/^\( *\)title=\(.*\),/\1title={\\textbf\2},/' in
@inproceedings{lechartier2014static,
  title={\textbf{Static and Dynamic Modeling of a PEMFC for Prognostics Purpose}},
  author={Lechartier, Elodie and Gouriveau, Rafael and Pera, Marie-Cecile and Hissel, Daniel and Zerhouni, Noureddine},
  booktitle={Vehicle Power and Propulsion Conference (VPPC), 2014 IEEE},
  pages={1--5},
  year={2014},
  organization={IEEE}
}
@inproceedings{zein2013statistical,
  title={\textbf{Statistical approach to online prognostics of turbine engine components}},
  author={Zein-Sabatto, Saleh and Bodruzzaman, Jabir and Mikhail, Mervat},
  booktitle={Southeastcon, 2013 Proceedings of IEEE},
  pages={1--6},
  year={2013},
  organization={IEEE}
}

相关内容