SED:如果第二个字段匹配模式,则将最后一个字段替换为可变中的值
设置
我有一个带有一些行不完整的文件的文件,例如下面的示例中的第二个文件,
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.
我将完整句子存储在一个称为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.
所需输出的
变量中,我试图用<<代码> p7 如果第二个字段匹配1806.10
如下以下
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.
失败的尝试
cat file1.txt | sed -E "s/(7:\|1806\.10\|\|)(.*)$/\1${p7}/g" > file2.txt
不会替换p7
的第三个字段。
2。
cat file1.txt | sed -z "s/(7:\|1806\.10\|\|)(.*)$/\1${p7}/g" > file2.txt
给出错误sed:-e表达式#1,char 320:
s'命令的rhs rhs“无效参考\ 1。
3。
cat file1.txt | sed -z "s/7:\|1806\.10\|\|.*/7:\|1806\.10\|\|${p7}/g" > file2.txt
替换最后一个字段,但会擦除文件中的其他所有内容。
我在做什么错?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个简单的
awk
程序可以在同一过程中为您提供帮助。这实际上是一个awk
任务,在此处更容易处理与sed
IMHO相比。awk
需要运行的代码是:说明: 简单说明将是,将Shell变量
P7
传递到<代码> awk 其变量名是p7
。然后在主程序设置中作为P7。1
是awk
打印当前行的方法。This simple
awk
program could help you in same. This is really anawk
task, would be easier to handle in here compare tosed
IMHO.awk
code needed to be run would be:Explanation: Simple explanation would be, passing shell variable
p7
intoawk
whose variable name is alsop7
. Then in main program settingFS
andOFS
as|
and checking condition if 2nd field is1806.10
then setting last field as p7 to it.1
is anawk
ish way to print current line.使用
sed
Using
sed