Issue
I have some file with strings like:
Author = {A. Williams and A.~G. Clarke and M. Pourkashanian},
I need to rearrange the initials after surname, separated by commas like:
Author = {Williams, A. and Clarke, A.~G. and Pourkashanian, M.},
All names separated by keyword and
.
How to do this with sed
?
My attempt
sed.exe -i "/^ *author *=/ s/(\{|and )([A-Za-z]{2,}) ([A-Za-z]\.[A-Za-z]\.)\}/\1\3 \2/g" file.bib
Solution
This might work for you (GNU sed):
sed -r '/^Author/!b;h;s/.*\{(.*)\}.*/\1/;s/(\S+\.) (\S+)/\2, \1/g;G;s/(.*)\n(.*\{)[^}]*/\2\1/' file
Focus on only lines that begin Author
. Copy the line to the hold space, we will need the parts we don't change to reassemble with the parts that we do. Remove the front and the back of the line i.e. the parts before the {
and the parts after the }
. Globally switch the initials with the surname and introduce a ,
between them. Append the original line and then replace the original names and initials with the new formatted ones.
Answered By - potong