Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2003
    Posts
    959

    replace a string in a file

    Hello,

    What is the command to replace a string in a text file? Like changing all the IP addresses in the /etc/hosts without changing it one by one but with a command to replace 111.222.333.444 to 222.333.444.555

    Best Regards

  2. #2
    Join Date
    Jul 2003
    Location
    USA
    Posts
    134
    Try:

    replace old-string new-string -- /etc/hosts
    .: Signature not required nor available at present time :.

  3. #3
    Join Date
    Nov 2004
    Location
    Marietta PA
    Posts
    137
    Use sed. you can man sed.

    Not very hard to do.
    Digital Offensive
    http://www.digitaloffensive.com
    Take an offensive approach to Security know what your foes know!

  4. #4
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    1,082

  5. #5
    Join Date
    Apr 2003
    Posts
    959
    ahh!! thanks everyone~

  6. #6
    Join Date
    Jul 2001
    Location
    Singapore
    Posts
    1,790
    Code:
    perl -i.bak -pe 's/111.222.333.444/222.333.444.555/g' /etc/hosts
    OR
    Code:
    sed -i.bak -e 's/111.222.333.444/222.333.444.555/g' /etc/hosts
    Both the above commands does the job and save a backup file name ended with .bak in case you need to restore back the backup
    Giam Teck Choon
    :: Join choon.net Community today to share your tips and tricks on server issues please ::
    :: Singapore Dedicated Servers :: Singapore Virtual Private Servers :: Linux/FreeBSD Server Management ::

Posting Permissions

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