LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Remove first character with sed and grep (https://www.linuxquestions.org/questions/programming-9/remove-first-character-with-sed-and-grep-924115/)

r_jr 01-16-2012 06:38 PM

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.

:confused:

jschiwal 01-16-2012 06:51 PM

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

custangro 01-16-2012 07:13 PM

Quote:

Originally Posted by r_jr (Post 4576332)
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.

:confused:

You can use egrep...

Code:

egrep -v '^#rt|^#vt' rt1.txt

r_jr 01-16-2012 08:44 PM

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
:cool:

r_jr 01-16-2012 10:18 PM

Quote:

Originally Posted by custangro (Post 4576349)
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.

grail 01-16-2012 11:11 PM

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.

Ramurd 01-17-2012 12:32 AM

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

Reuti 01-17-2012 08:38 AM

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

custangro 01-17-2012 10:39 AM

Quote:

Originally Posted by r_jr (Post 4576444)
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

r_jr 02-03-2012 06:16 PM

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
:)

r_jr 02-03-2012 06:18 PM

Quote:

Originally Posted by grail (Post 4576467)
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.

grail 02-04-2012 03:37 AM

As others have indicated, there is no need to repeat the pattern:
Code:

sed -ri '/^#(b.*[vr]t)/\1/' rt1.txt


All times are GMT -5. The time now is 03:25 PM.