`:g/^./ .,/^$/-1 join`
Set variables...
md="/dev/mdX"
sd="/dev/sdYZ"
Make a backup of the partition table. This won't work with GPT!
sfdisk -d $sd > partitiontable
Mark as faulty and remove the partition from the RAID:
mdadm -f $md $sd
mdadm -r $md $sd
Get the scsi data
scsi="$(lsscsi | grep $sd | awk '{print $1;}' | sed -e 's/[][]//g' -e 's/:/ /g')"
Tell Linux to get rid of the device
echo "scsi remove-single-device $scsi" > /proc/scsi/scsi
Physically remove the old drive and insert the new one at this point, then tell Linux to add it:
echo "scsi add-single-device $scsi" > /proc/scsi/scsi
Copy the partition table back:
sfdisk $sd < partitiontable