Delete specific line from a file with sed


/ Published in: Bash
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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

You need to login to post a comment.