UNIX: remove specific line from file command


Following command will remove 5th line from `file.txt`:

$ sed -i "5 d" file.txt

You wonder when could it be useful? Here is an example:
You try to connect to a remote host via SSH, however you get an error like that:

WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
...
Offending key in ~/.ssh/known_hosts:5

One of the solutions here is to remove the offending key, which is taking the whole 5th line in known_hosts file, so this is exactly the case where you can use our command to remove line quickly and then retry the connection.

UNIX: remove specific line from file command

Leave a comment