LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-16-2012, 06:38 PM   #1
r_jr
Member
 
Registered: Feb 2006
Distribution: Ubuntu
Posts: 65

Rep: Reputation: 0
Question Remove first character with sed and grep


I am trying to go through a file and remove the '#' character on all lines that start with '#rt' and '#vt'. I tried

Code:
grep -i -e "rt" -e "vt" | sed -e 's/#//' rt1.txt
This does what I want, but only echoes it to the screen. I would actually like to remove the '#' inside the actual file. I am not a regular expressions expert so I thought I would ask for some help.

 
Old 01-16-2012, 06:51 PM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Don't use grep because it will only return matching lines.

Code:
sed -i '/^#rt/s/^#//;/^#vt/s/^#//' rt1.txt
This is actually 2 sed commands.
The /#rt/ & /^#vt/ before the command select lines matching the criteria you gave. The command part deletes the # beginning the line.

The -i option is for inplace editing. If you don't have GNU sed, redirect the output to a temporary file, and rename the file afterwards to replace the old file.
sed ... file >newfile
mv newfile >file

Last edited by jschiwal; 01-16-2012 at 06:54 PM.
 
Old 01-16-2012, 07:13 PM   #3
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , Solaris 10, RHEL
Posts: 1,935
Blog Entries: 1

Rep: Reputation: 188Reputation: 188
Quote:
Originally Posted by r_jr View Post
I am trying to go through a file and remove the '#' character on all lines that start with '#rt' and '#vt'. I tried

Code:
grep -i -e "rt" -e "vt" | sed -e 's/#//' rt1.txt
This does what I want, but only echoes it to the screen. I would actually like to remove the '#' inside the actual file. I am not a regular expressions expert so I thought I would ask for some help.

You can use egrep...

Code:
egrep -v '^#rt|^#vt' rt1.txt
 
Old 01-16-2012, 08:44 PM   #4
r_jr
Member
 
Registered: Feb 2006
Distribution: Ubuntu
Posts: 65

Original Poster
Rep: Reputation: 0
I made a slight mistake, the lines I am looking for start with the word #blacklist rt... They don't actually start with rt or vt.

Okay, well I figured it out finally and it seems to work okay.

Code:
sed -i '/^#.*rt/s/^#//;/^#.*vt/s/^#//' rt1.txt

Last edited by r_jr; 01-16-2012 at 10:09 PM.
 
Old 01-16-2012, 10:18 PM   #5
r_jr
Member
 
Registered: Feb 2006
Distribution: Ubuntu
Posts: 65

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by custangro View Post
You can use egrep...

Code:
egrep -v '^#rt|^#vt' rt1.txt
This did not work for me. All it did was echo all the lines that did not match.
 
Old 01-16-2012, 11:11 PM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,478

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
Not to be too picky but you do realise that your current solution will also match something like:
Code:
# This is part of the wrong solution
Portion in red will match what you have used.
 
Old 01-17-2012, 12:32 AM   #7
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 545

Rep: Reputation: 74
Quote:
You can use egrep...

Code:
egrep -v '^#rt|^#vt' rt1.txt
No, he can't; unless you can only remove the # part of that line in that case.

sed -i is the correct way to go.

Quote:
I made a slight mistake, the lines I am looking for start with the word #blacklist rt... They don't actually start with rt or vt.

Okay, well I figured it out finally and it seems to work okay.
Code:
sed -i '/^#.*rt/s/^#//;/^#.*vt/s/^#//' rt1.txt
Why not go for
Code:
sed -i /^#blacklist\ rt/s/^#//;/^#blacklist\ vt//' rt1.txt
Did this from the top of my head, the space my not have to be escaped; not 100% sure
 
Old 01-17-2012, 08:38 AM   #8
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,319

Rep: Reputation: 252Reputation: 252Reputation: 252
As GNU sed also accepts extended regular expressions it should be possible to put both in one statement:
Code:
sed -ri 's/^#(blacklist (rt|vt).*)/\1/' rt1.txt
 
Old 01-17-2012, 10:39 AM   #9
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , Solaris 10, RHEL
Posts: 1,935
Blog Entries: 1

Rep: Reputation: 188Reputation: 188
Quote:
Originally Posted by r_jr View Post
This did not work for me. All it did was echo all the lines that did not match.
I misread and just noticed that you wanted to remove the characters...not omit the lines.

Then yes sed is the way to go and the previous examples will do the trick

-C
 
Old 02-03-2012, 06:16 PM   #10
r_jr
Member
 
Registered: Feb 2006
Distribution: Ubuntu
Posts: 65

Original Poster
Rep: Reputation: 0
This is my first chance to respond for a couple of weeks now. Thanks for all the responses. I did notice my final solution was not quite what I was looking for as it uncommented any lines with rt and vt in them. So, I modified it slightly and now I have what I think will work properly. I have used it for a couple of weeks now and have not had any problems.

Code:
sed -i '/^#b.*rt/s/^#//;/^#b.*vt/s/^#//' rt1.txt

Last edited by r_jr; 02-03-2012 at 06:20 PM.
 
Old 02-03-2012, 06:18 PM   #11
r_jr
Member
 
Registered: Feb 2006
Distribution: Ubuntu
Posts: 65

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Not to be too picky but you do realise that your current solution will also match something like:
Code:
# This is part of the wrong solution
Portion in red will match what you have used.
yes, I did notice and it has been correct now. Thanks.
 
Old 02-04-2012, 03:37 AM   #12
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,478

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
As others have indicated, there is no need to repeat the pattern:
Code:
sed -ri '/^#(b.*[vr]t)/\1/' rt1.txt
 
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Using grep with the accent character present eddyq Linux - Newbie 4 07-08-2011 04:46 PM
Help to remove script data from file using grep & sed djlane Programming 1 07-13-2010 08:10 AM
[SOLVED] Grep until certain character or pattern appears ohijames Programming 7 06-28-2010 08:38 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM


All times are GMT -5. The time now is 12:32 AM.

Main Menu
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