Setup
I have a file with some lines which are incomplete, like the second one in the following example
cat file1.txt
7:|18.01-18.05|Un cambio a la partida 18.01 a 18.05 de cualquier otro capítulo.
7:|1806.10|Un cambio a la subpartida 1806.10 de cualquier otra partida, siempre que el
7:|1806.20|Un cambio a la subpartida 1806.20 de cualquier otra partida.
I stored the full sentence in a variable called p7
echo $p7
Un cambio a la subpartida 1806.10 de cualquier otra partida, siempre que el azúcar no originaria del Capítulo 17 no constituya más del 35 por ciento en peso del azúcar y el polvo de cacao no originario de la partida 18.05 no constituya más del 35 por ciento en peso del cacao en polvo.
Desired output
I'm trying to replace the last field with p7 if the second field matches 1806.10 as follows
cat file2.txt
7:|18.01-18.05|Un cambio a la partida 18.01 a 18.05 de cualquier otro capítulo.
7:|1806.10|Un cambio a la subpartida 1806.10 de cualquier otra partida, siempre que el azúcar no originaria del Capítulo 17 no constituya más del 35 por ciento en peso del azúcar y el polvo de cacao no originario de la partida 18.05 no constituya más del 35 por ciento en peso del cacao en polvo.
7:|1806.20|Un cambio a la subpartida 1806.20 de cualquier otra partida.
Failed attempts
cat file1.txt | sed -E "s/(7:\|1806\.10\|\|)(.*)$/\1${p7}/g" > file2.txt
doesn't replace the third field for p7.
2.
cat file1.txt | sed -z "s/(7:\|1806\.10\|\|)(.*)$/\1${p7}/g" > file2.txt
gives the error sed: -e expression #1, char 320: invalid reference \1 on s' command's RHS`.
3.
cat file1.txt | sed -z "s/7:\|1806\.10\|\|.*/7:\|1806\.10\|\|${p7}/g" > file2.txt
replaces the last field but erases everything else in the file.
What am I doing wrong?