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 :confused: |
Don't use grep because it will only return matching lines.
Code:
sed -i '/^#rt/s/^#//;/^#vt/s/^#//' rt1.txt 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 |
Quote:
Code:
egrep -v '^#rt|^#vt' rt1.txt |
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 |
Quote:
|
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 |
Quote:
sed -i is the correct way to go. Quote:
Code:
sed -i /^#blacklist\ rt/s/^#//;/^#blacklist\ vt//' rt1.txt |
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 |
Quote:
Then yes sed is the way to go and the previous examples will do the trick -C |
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 |
Quote:
|
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. |