Find the answer to your Linux question:
Results 1 to 3 of 3
Hello, I have just changed a setting and I need to go through about 30 files within a directory and find all instances of a word, and change it to ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    24

    Can I use grep to find & replace text?

    Hello,

    I have just changed a setting and I need to go through about 30 files within a directory and find all instances of a word, and change it to another word.

    My intial though was GREP was the tool to use, however, I looked in the man pages and I can't find a "find & replace" option within it.

    Is there a way that I can use grep to look into every file within the directory and change the word?

    any help is greatly appreciated.

  2. #2
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    Sed might be what you need
    Code:
     sed 's/word1/word2/' yourfile.txt
    That will replace all occurences of word1 with word2 inside yourfile.txt.
    Last edited by smolloy; 07-18-2008 at 11:20 PM. Reason: typo in command
    Registered Linux user #388328 || Registered LFS user #15880
    AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

  3. #3
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    and dont forget the -i and g options for sed.
    see the man page
    Code:
    man sed
    Linux and me it's a love story

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...