我正在使用pinboard.in API来获取我当前书签的列表。结果如下所示:
<post href="https://www.nocc.meezy.com/doc/view.cgi?id=715" time="2013-02-11T17:38:10Z" description="Disk Errors Process Flow Chart" extended="" tag="nocc work" hash="a3419515b2e956e86886ba630b6028b7" meta="d793aeef6133a26e361695181eb57b9d" />
<post href="https://www.nocc.meezy.com/doc/view.cgi?id=39" time="2013-02-11T17:38:08Z" description="Using socat" extended="" tag="socat work" hash="fd60523bf841b2b95674a0e1d4401f4d" meta="5f2b6ad395fe4da05b2987d199b675ea" />
<post href="https://agora.meezy.com/wiki/Development_Tools" time="2013-02-11T17:38:06Z" description="Development Tools - meezyWiki" extended="" tag="devtools work" hash="dcf904433987a125c00a88bcaf31cad27" meta="5e744562282561390a0417223d323aee" />
我只对网址,说明和标签感兴趣,所以我希望结果如下:
https://www.nocc.meezy.com/doc/view.cgi?id=715 description="Disk Errors Process Flow Chart" tag="nocc work"
https://www.nocc.meezy.com/doc/view.cgi?id=39 description="Using socat" extended="" tag="socat work"
https://agora.meezy.com/wiki/Development_Tools description="Development Tools - meezyWiki" tag="devtools work
我对awk / cut / sed有一点了解但不足以告诉他们当描述和标记字段包含空格和不同数量的字符串时如何正确计算字段。
如果我的生活依赖于它,我可能会破解一些非常糟糕的解决方案,但我宁愿得到一个比我更了解他们的人的正确解决方案。
谢谢
答案 0 :(得分:1)
当您使用regex / awk / sed玩xml时,您应该知道风险。这是根据您的要求选择单行:
sed -r 's/^.*"(http)/\1/; s/" time=.*( desc)/ \1/; s/extended=.*( tag=")/\1/; s/hash=.*//' file
用你的例子测试:
kent$ sed -r 's/^.*"(http)/\1/; s/" time=.*( desc)/ \1/; s/extended=.*( tag=")/\1/; s/hash=.*//' file
https://www.nocc.meezy.com/doc/view.cgi?id=715 description="Disk Errors Process Flow Chart" tag="nocc work"
https://www.nocc.meezy.com/doc/view.cgi?id=39 description="Using socat" tag="socat work"
https://agora.meezy.com/wiki/Development_Tools description="Development Tools - meezyWiki" tag="devtools work"