-   Linux - Newbie (
-   -   change inside a file without opening (

hoi 08-20-2013 03:39 PM

change inside a file without opening
I have a shell file which text is

for a in 5.80 5.82 5.90 5.94 5.98 6.00 6.02 6.06 6.1
sed 's/5.85/$a/g' old_file > new_file

Basically I want to replace 5.85 by 5.80 then 5.82 and so on.
But in new_file it replace 5.85 by $a, that means $a does not work in 's/5.85/$a/g'.
How do I fix this problem?

Thank you

Firerat 08-20-2013 04:11 PM

use double quotes, and protect a with {}

sed "s/5.85/${a}/g"

and don't forget ;do done

for a in ....;do
  sed ..

SAbhi 08-20-2013 11:12 PM


sed 's/5.85/$a/g' old_file > new_file
That does not work because surrounded by '----' and a is not protected so when parsing the line the shell interprets $a as it is without substituting the value.

Solution is already provided by @Firerat.

hoi 08-21-2013 03:44 AM

Thanks Firerat for answer
Thanks SAbhi for explanation.

All times are GMT -5. The time now is 10:49 PM.