LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Replacing lines in files that contain special characters (https://www.linuxquestions.org/questions/linux-general-1/replacing-lines-in-files-that-contain-special-characters-734621/)

arizonagroovejet 06-21-2009 04:37 PM

Replacing lines in files that contain special characters
 
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.

bigearsbilly 06-21-2009 05:09 PM

man you are making life difficult
???
what chars?

e.g. for @ or #

perl -i.bak -pe 's/^#// if m/[@#]/' file_to_edit

ghostdog74 06-22-2009 01:45 AM

Code:

awk '/^#.*[@#]/{sub(/^#/,"")}1' file

Kenhelm 06-22-2009 09:19 PM

Spaces are being lost in the lines stored in variables when they are echoed into sed.
The variables should be in quotes or echo will squeeze runs of spaces down to a single space.
Code:

line='#      text[NORMAL]    = @text_color'

echo "$line"
#      text[NORMAL]    = @text_color

echo $line
# text[NORMAL] = @text_color



All times are GMT -5. The time now is 06:57 AM.