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.

  • 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
  • @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 unix.stackexchange.com/questions/129059/… . I've reopened and can't close again, can you do the honors? – Gilles 'SO- stop being evil' Dec 17 '14 at 23:34