Find the answer to your Linux question:
Results 1 to 10 of 10
Hi I need to replace this word https://$test with $test .... I have tried this using the sed command like sed -i 's/https://$test/$test/g' test.php .. but I am not getting ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Location
    Hyderabad
    Posts
    10

    Command to Find and Replace the word


    Hi

    I need to replace this word https://$test with $test.... I have tried this using the sed command like sed -i 's/https://$test/$test/g' test.php.. but I am not getting the desired output....
    Please can anyone help me how to do this.... Thanks.

  2. #2
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    848
    You were almost there with your sed command but you need to escape your forward slashes in the http:// so that sed doesn't get confused by them. You simply do this by preceding each one with a backslash like this:

    Code:
    sed -i 's/https:\/\/$test/$test/g' test.php
    Linux User #453176

  3. #3
    Just Joined!
    Join Date
    Mar 2009
    Location
    Hyderabad
    Posts
    10
    Thank you very much.. This command worked for me.. and I have one more question .. Can u pls say how to check the files list having this variable $test... I mean command to search the word in list of scripts.. I used find command find *.php -name $test but not working..... Thanks.

  4. #4
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    848
    Try using grep instead:

    Code:
    grep $test
    Linux User #453176

  5. #5
    Just Joined!
    Join Date
    Mar 2009
    Location
    Hyderabad
    Posts
    10
    Sorry, Not able to find the result from the above ... getting the msg like "Usage: grep [option]..pattern [file]".. but in this directory I am having the scripts using the $test variable.

  6. #6
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    848
    Sorry, try:

    Code:
    grep $test *.php
    Linux User #453176

  7. #7
    Just Joined!
    Join Date
    Mar 2009
    Location
    Hyderabad
    Posts
    10
    I have executed the above..But not getting any o/p.
    ..../TEST # grep $test *.php...

  8. #8
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    848
    You need to escape the $ symbol sorry. Also if you just want a list of filenames use the -l argument:

    Code:
    grep -l \$test *.php
    Linux User #453176

  9. #9
    Just Joined!
    Join Date
    Mar 2009
    Location
    Hyderabad
    Posts
    10
    Thank You.

  10. #10
    Linux Newbie tetsujin's Avatar
    Join Date
    Oct 2008
    Posts
    117
    Quote Originally Posted by Kieren View Post
    You were almost there with your sed command but you need to escape your forward slashes in the http:// so that sed doesn't get confused by them. You simply do this by preceding each one with a backslash like this:

    Code:
    sed -i 's/https:\/\/$test/$test/g' test.php
    Or, somewhat easier - by replacing the slashes in the "search-and-replace" pattern with another character, you can use slashes inside the pattern without escaping them:

    Code:
    sed -i "s#https://$test#$test#g" test.php

Posting Permissions

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