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?
CodePudding user response:
This simple awk program could help you in same. This is really an awk task, would be easier to handle in here compare to sed IMHO.
##Shell variable which has test value shown by OP.
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."
awk code needed to be run would be:
awk -v p7="$p7" 'BEGIN{FS=OFS="|"} $2==1806.10{$NF=p7} 1' Input_file
Explanation: Simple explanation would be, passing shell variable p7 into awk whose variable name is also p7. Then in main program setting FS and OFS as | and checking condition if 2nd field is 1806.10 then setting last field as p7 to it. 1 is an awkish way to print current line.
CodePudding user response:
Using sed
$ sed "/[^|]*|1806.10/{s/\(\([^|]*|\)\{2\}\).*/\1$p7/}" input_file
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.
