Find the answer to your Linux question:
Results 1 to 4 of 4
Hi All, The scenerio is that I want to update some file using a scrript as example: The file is: >cat profile Hi this is Yogender. Yogender is here. Is ...
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Posts
    10

    How to updatea file using a script

    Hi All,

    The scenerio is that I want to update some file using a scrript as example:

    The file is:

    >cat profile

    Hi this is Yogender.
    Yogender is here.
    Is that really yogender ?
    Ofcourse, he is Yogender.
    Infact, he should be yogender.

    Now i want to upadte "Yogender" in the file using a script where user can feed any name to replace "Yogender".

    The script si as follow:

    >sed_test
    echo -en "enter your name:"
    read name
    echo "$name"

    cat ~/aksutil/profile
    cat ~/aksutil/profile|sed 's/Yogender/$name/g' profile|tee profile1

    echo "Kindly check the file profile1"

    But after executing ./sed_test withinput name "Hello". I am getting the following output stored in the new file profile1 created by the script.
    >cat profile 1

    Hi this is $name.
    {$name} is here.
    Is that really yogender ?
    Ofcourse, he is $name.
    Infact, he should be yogender.

    However my expected output in Profile1 should be "Hello" instead of "$name".

    Could any one please help me?


    Thanks-
    Yogender

  2. #2
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    Use ${name} in your sed:

    Code:
    cat ~/aksutil/profile|sed 's/Yogender/${name}/g' profile|tee profile1
    Linux User #453176

  3. #3
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by Kieren View Post
    Use ${name} in your sed:

    Code:
    cat ~/aksutil/profile|sed 's/Yogender/${name}/g' profile|tee profile1
    This should not work.

    Single quotes prevent the shell to expand variables, use double quotes instead and the use of cat is redundant.

    Replace this line:

    Code:
    cat ~/aksutil/profile|sed 's/Yogender/$name/g' profile|tee profile1
    with:
    Code:
    sed "s/Yogender/$name/g" ~/aksutil/profile | tee profile1
    Regards

  4. #4
    Just Joined!
    Join Date
    Nov 2007
    Posts
    10
    Hi Kieren/Franklin,

    Thanks for your help. It's working fine now.

    Regards,
    Yogi

Posting Permissions

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