Q . How to Find and Replace text of any file using command line in linux.

@mit $ingh. asked, May 31 ' 2017

I have a file named test.txt.

I want to replace all abc text to xyz using command line in ubuntu.

  • linux
  • terminal
  • find-and-replace

1 Answers

sed -i 's/original/new/g' file.txt

Explanation:

  • sed = Stream EDitor
  • -i = in-place (i.e. save back to the original file)
  • The command string:

    • s = the substitute command
    • original = a regular expression describing the word to replace (or just the word itself)
    • new = the text to replace it with
    • g = global (i.e. replace all and not just the first occurrence)
  • file.txt = the file name

Leave a Ansewer