如何在 xmlstarlet 的 concat 中打印换行符
How to print newline inside concat of xmlstarlet
我想用换行符分隔我的输出,所以我尝试了:
xmlstarlet sel -t -m "//node01" -v 'concat(@title,"\n",script/code)' -n input.xml
然而,打印的是“\n”字面值,并且输出在同一行。如何在 concat() 函数中强制换行?
样本input.xml是:
<test>
<aaa>This is a test</aaa>
<node01 title="howdy">
<script>
<code>function idoit() {
console.log("hello world");
}
</code>
</script>
</node01>
</test>
而当我运行时,输出是:
howdy\nfunction idoit() {
console.log("hello world");
}
您可以在参数中给出文字换行符,其语法是 shell 特定的。
这应该适用于任何 POSIX sh
:
xmlstarlet sel -t -m "//node01" -v 'concat(@title,"
",script/code)' -n input.xml
在bash中,可以使用$'ANSI C Quoting':
xmlstarlet sel -t -m "//node01" -v $'concat(@title,"\n",script/code)' -n input.xml
另一个不是 shell 特定的技巧是设置一个 XPATH 变量,并将换行符作为值:
xmlstarlet sel -t --var nl -n -b -m "//node01" -v 'concat(@title,$nl,script/code)' -n input.xml
我想用换行符分隔我的输出,所以我尝试了:
xmlstarlet sel -t -m "//node01" -v 'concat(@title,"\n",script/code)' -n input.xml
然而,打印的是“\n”字面值,并且输出在同一行。如何在 concat() 函数中强制换行?
样本input.xml是:
<test>
<aaa>This is a test</aaa>
<node01 title="howdy">
<script>
<code>function idoit() {
console.log("hello world");
}
</code>
</script>
</node01>
</test>
而当我运行时,输出是:
howdy\nfunction idoit() {
console.log("hello world");
}
您可以在参数中给出文字换行符,其语法是 shell 特定的。
这应该适用于任何 POSIX
sh
:xmlstarlet sel -t -m "//node01" -v 'concat(@title," ",script/code)' -n input.xml
在bash中,可以使用$'ANSI C Quoting':
xmlstarlet sel -t -m "//node01" -v $'concat(@title,"\n",script/code)' -n input.xml
另一个不是 shell 特定的技巧是设置一个 XPATH 变量,并将换行符作为值:
xmlstarlet sel -t --var nl -n -b -m "//node01" -v 'concat(@title,$nl,script/code)' -n input.xml