Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 06-21-2009, 05:37 PM   #1
Senior Member
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,093

Rep: Reputation: 197Reputation: 197
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:

#	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.


cat file_containing_the_lines | while read line;do
     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;
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.
Old 06-21-2009, 06:09 PM   #2
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
man you are making life difficult
what chars?

e.g. for @ or #

perl -i.bak -pe 's/^#// if m/[@#]/' file_to_edit
Old 06-22-2009, 02:45 AM   #3
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
awk '/^#.*[@#]/{sub(/^#/,"")}1' file
Old 06-22-2009, 10:19 PM   #4
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 336

Rep: Reputation: 141Reputation: 141
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.
line='#       text[NORMAL]    = @text_color'

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

echo $line
# text[NORMAL] = @text_color


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash; editting config files and replacing lines jamescondron Programming 1 01-07-2009 07:17 PM
Copying files with special characters cornish Linux - Newbie 13 12-21-2007 12:22 PM
Searching for files with special characters Yig Linux - Newbie 4 11-08-2007 06:53 PM
using sed to insert lines with special characters disorderly Linux - Software 26 04-20-2006 06:30 PM
removing special lines from files Prasun1 Linux - General 3 09-11-2005 06:16 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:37 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration