I want to un-comment (remove the fist #) from a bunch of lines in a file where the lines contain various special characters (as far as regular expressions are concerned) and I can't figure out how to do it in a bash script.
Example of lines:
Code:
# text[NORMAL] = @text_color
# text[PRELIGHT] = lighter (@selected_bg_color) # Makes prelighted text colored.
# fg[INSENSITIVE] = "#666666" # Color for insensitive text.
What I can't figure out is how to actually modify the line in the file. The special characters always screw up the replacement for at least some of the lines. I've managed to get something (below, note various commented out bits indicate various experiments) which works for lines which don't have a () characters in them, but am now stuck on how to get those lines to be replaced. Can anyone tell me how to do that?
It occurs to me I could write the modified lines to a another file, then replace the original file with the new one, but that seems very inefficient.
Code:
#!/bin/bash
cat file_containing_the_lines | while read line;do
newline=${line/\#/}
newline_escape=$(echo $newline | sed 's!\([()]\)!\\\1!g');
#newline_escape=$(echo $newline | sed 's!\([]\*\$\#\/&[]\)!\\\1!g');
line_escape=$(echo $line | sed 's!\([()]\)!\\\1!g');
#line_escape=$(echo $line | sed 's!\([]\*\$\#\/&[]\)!\\\1!g');
#echo $line_escape;
#echo $newline_escape;
#sed -i "s/$line_escape/$newline_escape/" file_containing_the_lines;
# perl -p -i -e "s/\Q$line_escape\E/$newline_escape/" file_containing_the_lines;
perl -p -i -e "s/\Q$line\E/$newline/" file_containing_the_lines;
done
The file actually contains a bunch of lines that I do not want to uncomment, hence my script also has some stuff in that allows identifying of which lines do and do not want to be uncommented. I've simplified the code show above down to just the part I'm having trouble with.