是否有任何方法可以将管道输出重定向为文件?

发布于 2025-01-14 15:36:08 字数 1105 浏览 3 评论 0原文

我使用 xmllint 来选择节点,我的测试目的 1.xml 看起来像

<resources>
   <item>
        <label>LABEL</label>
        <value>VALUE</value>
        <description>DESCRIPTION</description>
   </item>
   <item>
        <label>LABEL</label>
        <value>VALUE</value>
        <description>DESCRIPTION</description>
   </item>
</resources>
$ xmllint --xpath '/resources/item/value' 1.xml
<value>VALUE</value><value>VALUE</value>

上面的这个命令运行良好。

然后我尝试与管道|结合,发生错误

$ cat 1.xml | xmllint --xpath '/resources/item/value'
Usage : xmllint [options] XMLfiles ...
...(help info)

我想原因是管道传输过程cat的输出作为流,但是xmllint 只能接收文件路径作为参数。那么,有什么办法可以解决这个问题呢?或者也许有其他选择?

当然。如果我的猜测有错,指出真正的原因对我也很有帮助。

抱歉

我的英语很差。请原谅语法或打字错误。我正在尽力改进。

I'm use xmllint to select node, and my test purposed 1.xml is looks like this

<resources>
   <item>
        <label>LABEL</label>
        <value>VALUE</value>
        <description>DESCRIPTION</description>
   </item>
   <item>
        <label>LABEL</label>
        <value>VALUE</value>
        <description>DESCRIPTION</description>
   </item>
</resources>
$ xmllint --xpath '/resources/item/value' 1.xml
<value>VALUE</value><value>VALUE</value>

Command likes above is work well.

And then i try to combine with pipe |, error occurred

$ cat 1.xml | xmllint --xpath '/resources/item/value'
Usage : xmllint [options] XMLfiles ...
...(help info)

I suppose the reason is pipe transmit process cat's output as a stream, but xmllint can only receive file path as argument. So, does any way to solve this problem? or maybe some alternative?

Of course. If my guess if fault, point at real reason is also pretty helpful to me.

sorry

My English is poor. Please excuse grammar or typing error. I'm trying my best to improve.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫雨熙 2025-01-21 15:36:08

我从未使用过 xmllint,但来自其 手册页 , 我可以看到:

xmllint 程序解析一个或多个 XML 文件,在命令行上指定为 XML-FILE(如果提供的文件名是 - 则为标准输入)

因此,以下内容应该有效:

xmllint --xpath '/resources/item/value' 1.xml

或者,如果您坚持输入应该来自标准输入,

xmllint --xpath '/resources/item/value' - <1.xml

I have never used xmllint, but from its man page, I can see:

The xmllint program parses one or more XML files, specified on the command line as XML-FILE (or the standard input if the filename provided is -

Therefore, the following should work:

xmllint --xpath '/resources/item/value' 1.xml

or, if you insist that the input should come via stdin,

xmllint --xpath '/resources/item/value' - <1.xml
无所的.畏惧 2025-01-21 15:36:08

作为替代方案,您可以通过 --shell 选项将 xmllint 命令传递到 stdin

echo -e 'cat /resources/item/value\nbye' | xmllint --shell 1.xml

或者

(echo 'cat /resources/item/value'; echo 'bye') | xmllint --shell 1.xml

As an alternative, you can pass xmllint command to stdin via --shell option

echo -e 'cat /resources/item/value\nbye' | xmllint --shell 1.xml

Or

(echo 'cat /resources/item/value'; echo 'bye') | xmllint --shell 1.xml
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文