0

I have many references in a BibTeX file as follows:

@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}
}

Link to the original file (the number of spaces in front of each title= entry varies).

How can I use regexes to change all the title part to be:

  title={\textbf{Static and Dynamic Modeling of a PEMFC for Prognostics Purpose}},
  • You should really modify the Bibliography style instead. – muru Oct 12 '15 at 6:14
1

Using sed:

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

To edit the file inplace instead of printing the edited file to stdout, add the -i option:

sed -i 's/^\( *\)title=\(.*\),/\1title={\\textbf\2},/' in
  • \( *\): matches and groups any number of characters at the start of the line;
  • title=: matches a ^title= string;
  • \(.*\),: matches and groups any number of any character before a , character greedily and a , character;
  • title={\\textbf\1},: replaces the match with a title={\textbf string followed by the captured group followed by a }, string;
% 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}
}
  • Your regex fails to handle commas in a title. Running sed 's/^title=\([^,]*\)/title={\\textbf\1}/' on a title with a comma messes it up: title={Static and Dynamic Modeling, and of a PEMFC for Prognostics Purpose}, is transformed into title={\textbf{Static and Dynamic Modeling}, and of a PEMFC for Prognostics Purpose}, – A.P. Oct 11 '15 at 11:03
  • Nope, not fixed. Now it messes up all lines, comma or not. Running your new regex sed 's/^title=\(.*,\)/title={\\textbf\1}/' transforms title={Static and Dynamic Modeling of a PEMFC for Prognostics Purpose}, to title={\textbf{Static and Dynamic Modeling of a PEMFC for Prognostics Purpose},}. – A.P. Oct 11 '15 at 11:15
  • @A.P. Absolutely. I'm probably tired because I didn't made a single thing right until now. Should be ok now, thanks – kos Oct 11 '15 at 11:22
  • No problem. Seems to work now. – A.P. Oct 11 '15 at 11:25
  • 1
    @OOzyPal Worked fine on my end. Perhaps you have windows line endings? If so, run dos2unix on your file first. You can also share your file on dropbox or something, I can take a look. – A.P. Oct 11 '15 at 12:01
3

This works fine:

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

This reformats all title parts and assumes there's just one title per line. It also does a backup of your file (with the .bak extension).

Update: If your input file has DOS/Windows line endings, do this:

  1. sudo apt-get install dos2unix
  2. dos2unix ProgRef.bib
  3. run the above regex
  • Why would it break the command if the trailing comma is anchored by $? Just tested with a comma in the title, works fine. Did you test my regex? If so, you got a test case to demonstrate the problem? – A.P. Oct 11 '15 at 10:56
  • That's all right, but you need to fix your regex regarding the commas, too. See my comment above. – A.P. Oct 11 '15 at 11:04
  • Yep, this was by design, but on second thought, I changed it to leave the leading whitespaces alone. – A.P. Oct 12 '15 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.