I usually omit to mention the -i option, unless specifically request, for safety. What if someone less experienced than you apply a command without testing to multiple files and scramble them all?!
Regarding your command, you're using the wildcard as the shell's file matching pattern. Instead in regular expressions it means
zero or more occurrences of the preceding character. Therefore using
you match a string with User followed by zero or more blank spaces. You have to use the following
to match User followed by a blank space and any number (zero included) of any character. Beware that in this case it matches anything till the end of the line:
Code:
sed -i '/^#/!s/User .*/User apache/' test.txt