LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   change inside a file without opening (https://www.linuxquestions.org/questions/linux-newbie-8/change-inside-a-file-without-opening-4175474050/)

hoi 08-20-2013 04: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 05:11 PM

use double quotes, and protect a with {}

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


and don't forget ;do done
Code:

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


SAbhi 08-21-2013 12:12 AM

Quote:

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 04:44 AM

Thanks Firerat for answer
Thanks SAbhi for explanation.


All times are GMT -5. The time now is 06:06 AM.