Does any one know how to find and replace text in a file. it can be implimented into a bash file. i would like to find the line and the output the text to it the file can only be opened with the root sudo account.

  • this is not a duplicate i would like to have a answer that explains it in more detial and what i have to put were i.e. the location of the text file the text to find and the text to replace
    – Jhondoe
    Jan 28 '15 at 17:18
  • @Jhondoe That's what manuals are for: man sed.
    – nanny
    Jan 28 '15 at 17:33
  • i did not know what sed was untill the first answer
    – Jhondoe
    Jan 28 '15 at 17:34

Basic example of sed usage using this as test.txt:

one two
three two four
two five

To replace two with foo in that file:

sed -i 's/foo/two/g' test.txt

What that means:

  • sed is the name of the command, you'll find lots of tutorials (e.g.) and other documentation online, in addition to man sed.
  • -i means edit a file in place.
  • 's/foo/two/g': the s indicates substitute first term for the second, the g (global) indicates for all instances in each line (not just the first). Always remember to 'enclose' this.

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