Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have a text file containing this:

0 0 -1 0
1 0 0 0
0 -1 0 0
1.5 0.0 1.0 1

and I want to replace the 1.0 to 2.0 so that it becomes:

0 0 -1 0
1 0 0 0
0 -1 0 0
1.5 0.0 2.0 1

so I use this command:

sed -i 's/'1.0'/'2.0'/g' /home/user1/file1.txt

or this:

sed -i 's/1.0/2.0/g' /home/user1/file1.txt

but this is the result that appears and I don't know how to fix this:

0 0 -2.0
2.0 0 0
0 -2.0 0
1.5 0.0 2.0 1
share|improve this question

1 Answer 1

up vote 2 down vote accepted

You need to escape dot in search pattern:

sed -i 's/1\.0/2.0/g' /home/user1/file1.txt
share|improve this answer
    
thanks for the answer! what if 1.0 and 2.0 were saved in variables $i and $j? where i has the value 1.0 and j has the value 2.0 –  Tak Mar 12 at 3:00
    
You should save 1.0 as 1\.0. –  cuonglm Mar 12 at 3:08
    
so is there any simple easy way to do that? because this code is in a loop and this value 1.0 changes from time to time and I don't want to save in the file1.txt as 1\.0 as this will affect the rest of the script. –  Tak Mar 12 at 3:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.