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 ...
- 03-07-2008 #1Just 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
- 03-07-2008 #2
Use ${name} in your sed:
Code:cat ~/aksutil/profile|sed 's/Yogender/${name}/g' profile|tee profile1Linux User #453176
- 03-07-2008 #3Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
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:
with:Code:cat ~/aksutil/profile|sed 's/Yogender/$name/g' profile|tee profile1
RegardsCode:sed "s/Yogender/$name/g" ~/aksutil/profile | tee profile1
- 03-07-2008 #4Just Joined!
- Join Date
- Nov 2007
- Posts
- 10
Hi Kieren/Franklin,
Thanks for your help. It's working fine now.
Regards,
Yogi


Reply With Quote
