Tag: trick

Linux sed trick – Remove line from file by number

I wanted to document this before I forgot it. To remove a single line from an existing file with sed, use:

sed -i '[num]d' [filename]

For example, to remove line 1 from ~/.ssh/known_hosts (my exact use case right now), type:

sed -i '1d' ~/.ssh/known_hosts

sed is a neat little tool. I use it to quickly copy and change Apache virtual hosts sometimes as well. I recommend learning a bit about it and its ‘s’ command as well.

P.S. If you don’t use the -i switch, sed will output the result after replacements instead of changing the file. You can pipe (|) this output to other programs or redirect (> or >>) it to a file.

Happy sed…ing.