0

I want to replace a string found in a file with another string, but both have a special character (in this example it is a . char) e.g 1.0 and 2.0

so this the is command currently used:

sed -i 's/1\.0/2\.0/g' /home/user1/file1.txt

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, how I can still the replace i with j?

EDIT:

As muru suggested, I used the double quotes as suggested and it works okay only if $i and $j were saved as 1.0 and 2.0 I doesn't work correctly if $i and $j where saved as 1.0 and 2.0 could anyone please advise how to fix this?

EDIT2:

As muru suggested, I followed the answer found here but still the result is not correct.

This is my shell file:

declare -a arr1=("1.0" "2.0" "3.0" "4.0")
ii=1.0
for i in "${arr1[@]}"
do
    str2=$i
    echo -e "\e[41m## str2 = $str2 ##\e[0m" 
    echo -e "\e[41m## $ii $str2 ##\e[0m"
    printf '%s\n' "$ii" "$str2" |
    file=/home/user1/text1.txt
    tmpfile="${TMPDIR:-/tmp}/$( basename "$file" ).$$"
    while read line
    do
      echo ${line/$ii/$str2}
    done < "$file" > "$tmpfile" && mv "$tmpfile" "$file"

    ii=$str2

done

this is my text1.txt file:

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

and this is the result:

0 0 -4.0
4.0 0 0
0 -4.0 0
1.5 0.0 2.5 1

and the correct result should be:

0 0 -1 0
1 0 0 0
0 -1 0 0
1.5 0.0 4.0 1
Tak
  • 966
  • 3
  • 13
  • 32
  • possible duplicate of [How to use variables in 'sed' command?](http://askubuntu.com/questions/76808/how-to-use-variables-in-sed-command) – muru Mar 12 '15 at 03:41
  • @muru thanks for that, I used the double quotes as suggested and it works okay only if `$i` and `$j` were saved as `1\.0` and `2\.0` I doesn't work correctly if `$i` and `$j` where saved as `1.0` and `2.0` could you please advise how to fix this? – Tak Mar 12 '15 at 03:52
  • There's no east way to make sed use fixed strings. You can process the variables to replace . with `\.`, or try http://stackoverflow.com/a/4160535/2072269. – muru Mar 12 '15 at 04:09
  • @muru I've updated my question so if you could please advise. – Tak Mar 12 '15 at 04:59
  • 2
    duplicate of http://unix.stackexchange.com/q/189651/4667 – glenn jackman Mar 12 '15 at 10:58

2 Answers2

2

Use perl instead. It has an interesting quotemeta feature in regular expressions where it will handle special characters like ., *, etc as plain characters

$ i=1.0  j=4.0
$ perl -pe "s/\Q$i/$j/g" text1.txt 
0 0 -1 0
1 0 0 0
0 -1 0 0
1.5 0.0 4.0 1

It's documented here: http://perldoc.perl.org/functions/quotemeta.html

glenn jackman
  • 17,625
  • 2
  • 37
  • 60
0

Sorry, I misunderstood a bit... this:

only covers the replacement string.

For exact matching in the search string, I don't think sed is your tool, you want the answer that @muru posted as well:

Good luck

dpb
  • 6,989
  • 1
  • 34
  • 60
  • Consider `110` vs `1.0`. – muru Mar 12 '15 at 04:08
  • Ah Thanks, you want to escape special characters in strings. Then you want this question/answer: https://stackoverflow.com/questions/407523 – dpb Mar 12 '15 at 04:09