1

I have two files: a.patterns contains patterns in a one per line way

"`"
"^"
"<"
"<("
"<)"
"<["
"="
">"
"_;|^+*+?@"

and b.data contains comma separated values.

1,1,0,"*",1
2,1,1,"*",1
3,0,0,"<)",0

The purpose is to substitute take each pattern matched in the csv file with that pattern's index (i.e., its line number).

The problem is that I load each pattern into a variable, and use sed to evaluate/replace it but frequently the patterns contain special characters.

...
while read line;
do
  counter=`expr $counter + 1`
  temp=`echo "$temp" | sed "s/$line/$counter/"`
done < a.patterns
...

My question: is there a way to tell sed to count the contents of the variable as literals? Ideally I would like to avoid inserting escape character for all special characters in the pattern file or alternative ways to achieve the same?

Thank you.

dr.doom
  • 111
  • 1
  • 2
  • Are you afraid of problems or have you actually observed a problem? If the latter: Give the code that causes it. I don't see how problems should arise there. Special characters affect variable assignments on the command line but not when reading data from a file. But I guess that the quotes in a.patterns will cause problems with sed. – Hauke Laging Dec 17 '14 at 23:10
  • See http://unix.stackexchange.com/questions/87380/using-a-reference-to-a-bash-string-variable-in-sed/87428#87428 – Gilles 'SO- stop being evil' Dec 17 '14 at 23:18
  • @Gilles, that other question had the variable on the RHS. When it's on the LHS, it's quite different (I'd expect there be a question covering that already as well though) – Stéphane Chazelas Dec 17 '14 at 23:32
  • @StéphaneChazelas Ooops, you're right. It's http://unix.stackexchange.com/questions/129059/how-to-ensure-that-string-interpolated-into-sed-substitution-escapes-all-metac . I've reopened and can't close again, can you do the honors? – Gilles 'SO- stop being evil' Dec 17 '14 at 23:34

0 Answers0