3 Replies - 650 Views - Last Post: 30 September 2013 - 05:25 PM Rate Topic: -----

#1 codinator  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 08-April 13

Linux Shell Script Replace Word(s) in File

Posted 30 September 2013 - 03:38 PM

I have a file name file.txt

    hello person 1
    hello person 2. hello person 3


im trying to write a shell script to replace the word 'hello' to 'goodbye'
so far this is what i got

    #!/bin/bash

function change(){
echo "change made"
}
    cat file.txt
change
exit 1



i can see the file, but how do i search in it and replace the word?
i did some research and found a function called sed but i cant seem to get it to work. anyone got any ideas?

This post has been edited by codinator: 30 September 2013 - 03:48 PM


Is This A Good Question/Topic? 1
  • +

Replies To: Linux Shell Script Replace Word(s) in File

#2 Gorian  Icon User is offline

  • ninja DIC
  • member icon

Reputation: 120
  • View blog
  • Posts: 1,681
  • Joined: 28-June 08

Re: Linux Shell Script Replace Word(s) in File

Posted 30 September 2013 - 03:59 PM

I'm not going to give you the answer, but it looks like you've done some work on your own, so I'd suggest researching sed's "search and replace" function.
http://www.brunolinu...e_with_Sed.html

Perl also has that functionality. You are on the right path :)
Was This Post Helpful? 1
  • +
  • -

#3 codinator  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 08-April 13

Re: Linux Shell Script Replace Word(s) in File

Posted 30 September 2013 - 04:29 PM

i got it thanks.

sed -i.bak "s/${word1}/${word2}/g" file.txt

This post has been edited by codinator: 30 September 2013 - 04:29 PM

Was This Post Helpful? 1
  • +
  • -

#4 Gorian  Icon User is offline

  • ninja DIC
  • member icon

Reputation: 120
  • View blog
  • Posts: 1,681
  • Joined: 28-June 08

Re: Linux Shell Script Replace Word(s) in File

Posted 30 September 2013 - 05:25 PM

Sweet, glad I could help :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1