How to append a newline after every match using xmlint --xpath

后端 未结 4 1050
别跟我提以往
别跟我提以往 2021-01-02 01:15

I have the following HTML code:


' \
  | xmlstarlet fo -H -R \
  | xmlstarlet sel -T -t -v '//textarea[@name="command"]' -n

If the Attempt to load network entity message from the second xmlstarlet invocation annoys you, just add 2>/dev/null at the very end to suppress it (at the risk of suppressing other messages printed to standard error).

The XMLStarlet options explained (see also the user's guide):

  • fo -H -Rformat the output, expecting HTML input, and recovering as much bad input as possible
    • this will add an root node, making the fragment in the OP's example valid XML
  • sel -T -t -v //xpath -nselect nodes based on XPath //xpath
    • output plain text (-T) instead of XML
    • using the given template (-t) that returns the value (-v) of the node rather than the node itself (allowing you to forgo using text() in the XPath expression)
    • finally, add a newline (-n)

Edit(s): Removed half-implemented xmllint --shell solution because it was just bad. Added an XMLStarlet example that actually works with the OP's data.

提交回复
热议问题