0

I used the following command in the bash to remove the last two lines of bash.bashrc (I added some bad lines) : sed '69,70d' /etc/bash.bashrc

But it doesn't delete the lines permanently as I would like.

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Simon
  • 5,502
  • 5
  • 38
  • 86

2 Answers2

0

This should work to remove the last two lines:

sed -n -e :a -e '1,2!{P;N;D;};N;ba' /etc/bash.bashrc

And then if you pipe it through | cat > /etc/bash.bashrc it will overwrite the file with the new shortened content:

sed -n -e :a -e '1,2!{P;N;D;};N;ba' /etc/bash.bashrc | cat > /etc/bash.bashrc
Giacomo1968
  • 24,837
  • 11
  • 67
  • 96
  • 1
    maybe you should use `-i` option. it's implemented in gnu sed. – ymonad Jun 09 '14 at 01:42
  • Thank you for your response, but it doesn't seem to work. Are there no simpler way to do it? – Simon Jun 09 '14 at 01:42
  • @user3683807 It could be that you need to run the command as `sudo`? So it would be `sudo sed -n -e :a -e '1,2!{P;N;D;};N;ba' /etc/bash.bashrc`. – Giacomo1968 Jun 09 '14 at 01:44
0

One solution is using head -n -<n> in combination of tee.

Although head does not support in place edit which is supported in sed -i , you can accomplish in place edit using tee command.

See: sed command find and replace in file and overwrite file doesn't work, it empties the file

head -n -2 /etc/bash.bashrc | sudo tee /etc/bash.bashrc >/dev/null
Community
  • 1
  • 1
ymonad
  • 11,040
  • 1
  • 36
  • 47