what am trying to do is to input the command sudo in a file with echo or something similar but not in the first line but in another line after a text
ie. I have this text

line[4]command=sh -c "service apache2 start"
and I want the sudo to be after "command=" how to do this with echo or anything else?

sed -i 's/command=/&sudo /' file

explanation: examining the file line-by-line, replace the text "command=" with "command=sudo ". The & in the replacement string will be whatever is matched from the left-hand side.


Just for completeness, note that one usually has several scriptable text editors available on a Unix or a Linux system, in addition to the stream editor sed. There's ex, for example:

ex file << END
%s/^command=/&sudo /

Or even ed (This is the GNU flavour.):

ed file << END
1,$s/^command=/&sudo /

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.