Delete single line from file
I quite often need to remove a single line from a file by its line number. The most common use case for me is the known_hosts file when I have reinstalled a system, I have in the past used vim and navigated to the line then removed it. This is all well and good but it gets to be a pain having to do it repeatedly, especially when you manage around 1000 servers and the get rebuilt frequently. Finally today I had had enough so wrote a little script to do this task easily. Hopefully someone else finds this useful
Its usage is : delline LINE FILE
#!/bin/bash LINE=$1 FILE=$2 if [ ! -f $FILE ] ; then echo "can't read $FILE: No such file or directory" exit 1 fi if [ `expr $LINE + 1 2> /dev/null` ] ; then sed -i "${LINE}d" $FILE else echo $LINE is not numeric exit 1 fi