Tool for adding index automatically to tex's files from a list

Is there any dedicated tool (script or LaTeX's package) for adding the \index command (or other command) to many LaTeX's files? The idea is to declare the list of the requested index such

Henri Bergson|Bergson, Henri
bergson|Bergson, Henri


The first word will be the pattern on which the \index command will be applied et the second will be the content of the \index command.

The idea will be also to avoid to applied the \index on certain case: for instance if the pattern is in a \cite command (\cite{bergson}).

Finally I propose a small script for doing this. It is written in shell using perl.

It must be placed in a folder on which all the tex file are situated. A list_index.txt file must be placed at the same level of the script. In this file you can add on line per author with the following format:

Henri Bergson|Bergson, Henri|B
bergson|Bergson, Henri|B
charlie chaplin|Chaplin, Charlie
chaplin|Chaplin, Charlie
lilian gish|Gish, Lilian
gish|Gish, Lilian


The syntax must be:

• First column: the pattern on which you want to add an index entry
• Second column: the form on the index entry
• Third column (optional): the position you want in the index (if you want to force a specific location.


How it works:

Each time the pattern is find in the tex's files (unsensitive case), it is replace by the command \md{<<pattern>>}{<< corresponding second column>>}. For example, ìn the text bergson will be replaced by \md{bergson}{B@Bergson, Henri} and charlie chaplin will be replaced by \md{bergson}{Chaplin, Charlie}.

The script avoid to add \md command in inappropriate place such as \cite, \footcite, footfullcite, \md, \ref, \pageref and \includegraphics. This list must be adapted in the perl lines of the script.

Bash's script:

#!/bin/bash

listeIdx=list_index.txt
cmdIndex='\\md'

while IFS= read -r var || [ -n "$var" ] do entryIx=echo$var|cut -d'|' -f1
keyIx=echo $var|cut -d'|' -f2 specIx=echo$var|cut -d'|' -f3
#
if [[ -z "${keyIx// }" ]];then echo$keyIx
continue
else
echo -e "\n\nEntry: $entryIx || Key:$keyIx"
for file in ls *.tex
do
if [ "$file" != "manuscrit.tex" -a "$file" != "Couverture.tex" -a "$file" != "resume.tex" -a "$file" != "titre.tex" ];then
echo $file if [[ -z "${specIx// }" ]];then
perl -i.bak -lpe 's/(\\pageref([^}]*)|\\ref([^}]*)|\\label([^}]*)|\\includegraphics([^}]*)|\\footfullcite([^}]*)|\\footcite([^}]*)|\\cite([^}]*)|\\md{(.*?)}([^}]*))(*SKIP)(*FAIL)|\b'"${entryIx}"'\b/'"${cmdIndex}"'{$&}{'"$keyIx"'}/ig' $file else perl -i.bak -lpe 's/(\\pageref([^}]*)|\\ref([^}]*)|\\label([^}]*)|\\includegraphics([^}]*)|\\footfullcite([^}]*)|\\footcite([^}]*)|\\cite([^}]*)|\\md{(.*?)}([^}]*))(*SKIP)(*FAIL)|\b'"${entryIx}"'\b/'"${cmdIndex}"'{$&}{'"$specIx"'\@'"$keyIx"'}/ig' $file fi fi done fi done <$listeIdx