Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何替换html属性值(shell/bash)中的字符?_Bash_Shell_Awk_Sed - Fatal编程技术网

如何替换html属性值(shell/bash)中的字符?

如何替换html属性值(shell/bash)中的字符?,bash,shell,awk,sed,Bash,Shell,Awk,Sed,很抱歉这个愚蠢的问题,我整个下午都被这个简单的问题难住了。因此,我有一个示例文本文件,其中包含: <product productId="123456" description="good apple, very green" publicPriceTTC="5,07" brand-id="152" /> <product productId="123457" description="fresh orange, very juicy" publicPriceTTC="12,4

很抱歉这个愚蠢的问题,我整个下午都被这个简单的问题难住了。因此,我有一个示例文本文件,其中包含:

<product productId="123456" description="good apple, very green" publicPriceTTC="5,07" brand-id="152" />
<product productId="123457" description="fresh orange, very juicy" publicPriceTTC="12,47" brand-id="153" />
<product productId="123458" description="big banana, very yellow" publicPriceTTC="5,07" brand-id="154" />

我想将此文件修改为:

<product productId="123456" description="good apple, very green" publicPriceTTC="5.07" brand-id="152" />
<product productId="123457" description="fresh orange, very juicy" publicPriceTTC="12.47" brand-id="153" />
<product productId="123458" description="big banana, very yellow" publicPriceTTC="5.07" brand-id="154" />

基本上,我需要在“publicPriceTTC”的所有值中用“.”(点)替换“,”(逗号)。这里的诀窍是其他属性的值中可能有逗号(本例中为“description”)。我想sed或awk可以做到,但我没能做到


有人能帮我吗?非常感谢您的帮助。

如果您搜索逗号以替换为点,您将执行非常粗略的搜索/替换。试试更特别的。使用sed,假设您的输入文件名为
xml

sed -E 's/(publicPriceTTC="[0-9]+),([0-9]+")/\1.\2/' xml
您可能知道sed有命令
s/
。我们用这个

-E
选项触发扩展正则表达式的使用。因此,
s
表达式匹配引号内的整个标记+“=”+数字,并使用括号将其中的位用作替换的一部分
\1
表示括号块之间的第一位<代码>\2为第二个


当然,您可以使搜索更加健壮,以处理标记和等号之间的空白等等。

如果您搜索逗号以替换为点,您将执行非常粗略的搜索/替换。试试更特别的。使用sed,假设您的输入文件名为
xml

sed -E 's/(publicPriceTTC="[0-9]+),([0-9]+")/\1.\2/' xml
您可能知道sed有命令
s/
。我们用这个

-E
选项触发扩展正则表达式的使用。因此,
s
表达式匹配引号内的整个标记+“=”+数字,并使用括号将其中的位用作替换的一部分
\1
表示括号块之间的第一位<代码>\2为第二个


当然,您可以使搜索更加健壮,以处理标记和等号之间的空白等问题。

解决此问题的awk解决方案可能是:

awk '/<product/{for(i=1;i<=NF;i++){if($i~/^publicPriceTTC="/)sub(/,/,".",$i)}}1' file.xml
但是。。。你在这里解决了错误的问题。像sed和awk这样基于正则表达式处理文件的工具不是XML解析器。无论是Javier的sed解决方案还是我的awk解决方案,都可能会意外地弄错某些东西,或者遗漏完全有效的XML文件中的某些东西

我建议您考虑使用python、perl、ruby、php或其他支持原生XML的语言

例如,将输入转换为实际的XML,如下所示:

<p>
<product productId="123456" description="good apple, very green" publicPriceTTC="5,07" brand-id="152" />
<product productId="123457" description="fresh orange, very juicy" publicPriceTTC="12,47" brand-id="153" />
<product productId="123458" description="big banana, very yellow" publicPriceTTC="5,07" brand-id="154" />
</p>
或分开阅读(和评论):


解决此问题的awk解决方案可能是:

awk '/<product/{for(i=1;i<=NF;i++){if($i~/^publicPriceTTC="/)sub(/,/,".",$i)}}1' file.xml
但是。。。你在这里解决了错误的问题。像sed和awk这样基于正则表达式处理文件的工具不是XML解析器。无论是Javier的sed解决方案还是我的awk解决方案,都可能会意外地弄错某些东西,或者遗漏完全有效的XML文件中的某些东西

我建议您考虑使用python、perl、ruby、php或其他支持原生XML的语言

例如,将输入转换为实际的XML,如下所示:

<p>
<product productId="123456" description="good apple, very green" publicPriceTTC="5,07" brand-id="152" />
<product productId="123457" description="fresh orange, very juicy" publicPriceTTC="12,47" brand-id="153" />
<product productId="123458" description="big banana, very yellow" publicPriceTTC="5,07" brand-id="154" />
</p>
或分开阅读(和评论):

这将在GNU上工作

sed  's/\(publicPriceTTC="[0-9]*\),/\1./' fileName
这将在GNU上起作用

sed  's/\(publicPriceTTC="[0-9]*\),/\1./' fileName

在awk中使用sub就足够了

awk '{sub(/,/,".",$7)}1' file

在awk中使用sub就足够了

awk '{sub(/,/,".",$7)}1' file

非常感谢你。我看你的解决方案也行。我知道用sed/awk解析XML文件是个坏主意,但我甚至不能用xmlstarlet编辑文档(该命令只返回“killed”)。我从未使用过xmlstarlet,但如果该程序的二进制文件正在转储内核或被某种信号杀死,那么可能是二进制文件不正确,或者安装失败。但是有很多工具知道如何使用xml。。。无需将自己限制为一人。:)非常感谢你。我看你的解决方案也行。我知道用sed/awk解析XML文件是个坏主意,但我甚至不能用xmlstarlet编辑文档(该命令只返回“killed”)。我从未使用过xmlstarlet,但如果该程序的二进制文件正在转储内核或被某种信号杀死,那么可能是二进制文件不正确,或者安装失败。但是有很多工具知道如何使用xml。。。无需将自己限制为一人。:)没问题max。请注意,使用XML解析器更可靠,但如果您知道XML文件具有特定格式,则使用搜索/替换方法会更快。没问题max。请注意,使用XML解析器更可靠,但如果您知道XML文件具有特定格式,则使用搜索/替换方法会更快。