Find the answer to your Linux question:
Results 1 to 3 of 3
My file.txt contains following: >> file.txt << Code: line one goes here line two is here and so on....... i want to replace the text in the file "one" with ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2010
    Posts
    5

    Dynamic command


    My file.txt contains following:

    >> file.txt <<
    Code:
    line one goes here
    line two is here
    and so on.......

    i want to replace the text in the file "one" with "hundred" using a shell script. My shell script contains the following command:

    >> replaceText.sh<<
    Code:
    sed -i 's/one/$1/g' file.txt
    I am calling it using:
    Code:
    ./replaceText hundred
    Instead of replacing "one" with "hundered" the shell script replaces it with $1.

    How can I get this working?

  2. #2
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    152
    Quote Originally Posted by nitalc View Post
    >> replaceText.sh<<
    Code:
    sed -i 's/one/$1/g' file.txt
    Change your single quotes (') for double quotes (") to make:

    Quote Originally Posted by nitalc View Post
    >> replaceText.sh<<
    Code:
    sed -i "s/one/$1/g" file.txt
    Bash will not change any text within single quotes (e.g. expanding $1 into the first argument), but it will expand text in double quotes.

  3. #3
    Just Joined!
    Join Date
    Oct 2010
    Posts
    5
    That worked . Thanks

  4. $spacer_open
    $spacer_close

Posting Permissions

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