seddocbook

sed replace from csv include last character of search term


I am trying to replace a list of words found in a csv file with index markup (docbook). The csv is in this format:

testword[ -?],testword<indexterm><primary>testword</primary></indexterm>

This finds all occurrences of the testword with punctuation at the end. This part works. However, I need the final punctuation mark to be included in the replace part of the sed command.

sed -e 's/\(.*\)/s,\1,g/' index.csv > index.sed

sed -i -f index.sed file.xml

So e.g. This is a testword, in a test. Would get replaced with This is a testword,<indexterm><primary>testword</primary></indexterm> in a test.


Solution

  • Problem is the string in the csv file that steers the proces, here you loose the punctuation. Replacing the: testword[ -?],testword<indexterm><primary>testword</primary></indexterm> by: testword\([ -?]\),testword\1<indexterm><primary>testword</primary></indexterm>

    Would already solve your problem.