Find the answer to your Linux question:
Results 1 to 2 of 2
I am creating a sort of database. 1 thing i want to allow the user to do is enter a word within the database they want to replace, then they ...
  1. #1
    Just Joined!
    Join Date
    Apr 2008
    Posts
    4

    user input swapping

    I am creating a sort of database. 1 thing i want to allow the user to do is enter a word within the database they want to replace, then they have to enter the word they want to replace it with, and once this is done, the swap should take place. The code i am trying to do this with is:

    echo please enter the word to be replaced
    read str
    grep -i $str database

    echo new word
    read no
    sed 's/str/no/g' database > database2
    mv database2 database

    This doesnt seem to work though, can any1 help me out please.

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    This certainly does not work. You are relying on sed to do your replacement, but look at the line you have given it:
    Code:
    sed 's/str/no/g' database
    This means "look for every occurrence of 'str' and replace it with 'no'"

    What you actually want is:
    Code:
    sed "s/$str/$no/g"
    Two changes:

    1) Use the variables $str and $no rather than the literal strings "str" and "no".

    2) Single Quotes (') to Double Quotes ("). Single quotes do not allow for variable interpolation.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

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