Rename Raspberry Pi

I need to edit both the hostname file AND the hosts file when I am renaming the RPi I am running my project on. The end user will use my Xojo project to use the touchscreen to create a new name. When I wrote the code, I did not know that I needed to change the hosts file also. Can anyone help me with the code to change the hosts file? I am struggling since the Hosts file looks like this

[quote] localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters raspberrypi[/quote]
and I need to change the last line only.

My current code is this:

Dim PiName As String Dim myshell As New Shell PiName="sudo hostnamectl set-hostname """" + NewMachineName.Text +"""" If TargetARM Then myshell.mode = 0 myshell.execute PiName myshell.Execute "sudo reboot" myshell.Close End If
Thanks in advance!

Do with shell / sed:

sudo sed -i -e 's/\\ raspberrypi/\\ newname/g' /etc/hosts

Thank you so very much!!! I wouldn’t have solved that on my own.
I ended up getting it to work AND made an adjustment so they can rename the unit more than once.

Dim PiName2 As String

PiName2="sudo sed -i -e 's/" + Production.ComputerNameActual.Text +"/" + NewMachineName.Text +"/g' /etc/hosts"    

myshell.execute PiName2

Great! glad to help.