Artificial intelligent assistant

Find a text in a file and Insert a text after it I have a text file. The text file contains the below content. ServerName ServerAdmin webmaster@localhost DocumentRoot I need to insert a text `www.mydomain.com`, after the `ServerName`. Is there any command to do this?

Using `sed`:


sed 's/^\(ServerName\)$/\1 www.mydomain.com/' file.txt


The captured group, `\(ServerName\)` is used in the replacement pattern as `\1`.

Editing the file in place, with backup, assuming the GNU, `ssed`, busybox or some BSD implementations of `sed`:


sed -i.bak 's/^\(ServerName\)$/\1 www.mydomain.com/' file.txt


The original file will be kept as `file.bak` and the modified file will be `file.txt.bak`.

Editing in place, without backup (GNU, `ssed` or `busybox` only):


sed -i 's/^\(ServerName\)$/\1 www.mydomain.com/' file.txt


(for BSDs, use `sed -i '' 's/...`).

* * *

Even shorter, without any captured group:


sed -i 's/^ServerName$/& www.mydomain.com/' file.txt


Here `&` will be replaced by the match.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy e4982ca39c8819acdd34c5d4200f011c