Posted By

Juanje on 05/08/08


Tagged

sed


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

benpjohnson


Delete specific line from a file with sed


 / Published in: Bash
 

For deleting a line from a file without open the file you can use sed.

For example, it has been very useful many times for me after anoying messages from ssh when a host I use to connect change the IP...

And sometimes you don't know the line number but you know a specific word from this line.

  1. # delete the line 18 from '~/.ssh/known_hosts' file
  2. sed -i '18 d' ~/.ssh/known_hosts
  3.  
  4. # also
  5. sed -i 18d ~/.ssh/known_hosts
  6.  
  7. # delete few lines
  8. # delete 6 lines from line 8
  9. sed -i 8,+6d file.txt
  10.  
  11.  
  12. # delete the line where is 'TO DELETE'
  13. sed -i '/TO DELETE/ d' file.txt

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Tenzer on May 9, 2008

You could also use ssh-keygen -R [host] to clean up the known_hosts file :)

Posted By: Juanje on May 30, 2008

I did't know. Thanks for the tip! :-)

You need to login to post a comment.