How Do I Grep a Word and Replace It in Linux?
Contents

How do I replace a word using grep?

No, you cannot replace a word with grep : grep looks for lines matching the expression you give it and prints those out (or with -v prints out the lines not matching the expression).

How do you replace a word in Linux?

The procedure to change the text in files under Linux/Unix using sed:

  1. Use Stream EDitor (sed) as follows:
  2. sed -i ‘s/old-text/new-text/g’ input. …
  3. The s is the substitute command of sed for find and replace.
  4. It tells sed to find all occurrences of ‘old-text’ and replace with ‘new-text’ in a file named input.
David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest