LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 06-21-2009, 05:37 PM   #1
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,032

Rep: Reputation: 174Reputation: 174
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.
 
Old 06-21-2009, 06:09 PM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
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
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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

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

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

echo $line
# text[NORMAL] = @text_color
 
  


Reply


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


All times are GMT -5. The time now is 11:30 PM.

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