Ubuntu – Sed replace specific line in file

bashcommand linescriptssed

I'm building a bash script for my virtual machine and I would like to know how to replace a specific line in this document:

[base]

## uncomment and set autologin username to enable autologin
# autologin=dgod

## uncomment and set timeout to enable timeout autologin,

## the value should >=5

# timeout=10

## default session or desktop used when no systemwide config

# session=/usr/bin/startlxde

this line:

# autologin=dgod

I want to change to this

autologin=ubuntu

I have tried with "tee" and "sed" but couldn't make it work.
This should be very easy for someone who works with bash scripts more often than me.

Best Answer

It is straightforward. Use the s command in sed to search and replace.

sed 's/# autologin=dgod/autologin=ubuntu/' /path/to/file

If you see what you want, add -i to change the file in place

sed -i 's/# autologin=dgod/autologin=ubuntu/' /path/to/file
Related Question