【问题标题】:Pipe urls from xml into seperate file in Linux, using sed, awk, cat or grep在 Linux 中,使用 sed、awk、cat 或 grep 将 xml 中的 url 管道传输到单独的文件中
【发布时间】:2014-07-27 23:25:54
【问题描述】:

根据下面的 xml 示例,我有一个包含许多产品的 xml 文件。

我想 grep 出此文档中的所有 url 并将它们传送到一个新文档中。例如,我想获取以下之间的网址:

<url></url>

并将它们通过管道传输到一个新的 txt 文件中,每个 url 在一个新行上。所以输出看起来像一个 url 列表,如:

http://www.example.com/nav/rooms/kitchens/kitchen-worktops/gemstone_solid_surface_worktops/-specificproducttype-worktops/Cooke-and-Lewis-Gemstone-Triassic-Worktop-3050mm-13128613
http://www.example.com/nav/fix/nails-screws-fixings-hardware/furniture-hardware/legs___supports/-specificproducttype-furniture_legs/Rothley-Furniture-Leg-Angled-L501XN-Brushed-Nickel-Effect-H128mm-9281999
http://www.example.com/nav/fix/electrical/cable-management/cable_clips/Corelectric-Clips-Cable-Round-Polybag-Pk20-11348134
http://www.example.com/nav/fix/power-tool-accessories/router-bits/jointing_biscuits/Trend-T-Tech-Beech-Biscuit-No-10-TT-BSC-10-100-Pack-9288386
etc... 

这是 xml 的一个示例,对于很多产品,这会重复很多次:

<product>
                          <id>13128613</id>
                          <name>Cooke &amp; Lewis Gemstone Triassic Worktop 3050mm</name>
                          <categoryId>9372151</categoryId>
                          <features>Edged 1 long, 2 short sides, No templating required reducing fitting complexities, time and cost, This stunning design is made from 85% recycled material including glass and shell, supporting environmental sustainability, A 6mm solid material bonded to a 28mm solid chipboard core, backed with a moisture resistant balance paper for complete water resistance, A hard surface that is resistant to daily wear and tear</features>
                          <url>http://www.example.com/nav/rooms/kitchens/kitchen-worktops/gemstone_solid_surface_worktops/-specificproducttype-worktops/Cooke-and-Lewis-Gemstone-Triassic-Worktop-3050mm-13128613</url>
                          <productHierarchy>Rooms &gt; Kitchens &gt; Kitchen Worktops &gt; Gemstone Solid Surface Worktops &gt; Worktops</productHierarchy>
                          <quantity/>
                          <sku>
                                    <id>13619319</id>
                                    <name>Cooke &amp; Lewis Gemstone Triassic Worktop 3050mm</name>
                                    <description>A 6mm solid material bonded to a 28mm high performance chipboard core, Cooke &amp; Lewis Gemstone is the perfect green choice, formulated with 85% recycled material.</description>
                                    <ean>5397007119039</ean>
                                    <condition>new</condition>
                                    <price>582.00</price>
                                    <wasPrice/>
                                    <deliveryCost>0.0</deliveryCost>
                                    <deliveryTime>Delivery usually within 5 weeks</deliveryTime>
                                    <stockAvailability>1</stockAvailability>
                                    <skuAvailableInStore>0</skuAvailableInStore>
                                    <skuAvailableOnline>1</skuAvailableOnline>
                                    <channel>Home Delivery Only</channel>
                                    <buyerCats>
                <catLevel0>KITCHENS</catLevel0>
                <catLevel1>SOLID SURFACE WORKTOPS</catLevel1>
                <catLevel2>SPEEDSTONE SOLID SURFACE</catLevel2>
            </buyerCats>
                                    <affiliateCats>
                <affiliateCat0>Home &amp; Garden</affiliateCat0>
            </affiliateCats>
                                    <manufacturersPartNumber/>
                                    <specificationsModelNumber/>
                                    <featuresBrand>Cooke &amp; Lewis Gemstone</featuresBrand>
                                    <imageUrl>http://example.com/is/image/5397007119039_001c_v001_zp</imageUrl>
                                    <thumbnailUrl>http://example.com/is/image/5397007119039_001c_v001_zp?$75x75_generic$=</thumbnailUrl>
                                    <skuNavAttributes>
                                              <ecoGrowFoods>false</ecoGrowFoods>
                                              <ecoDLME>false</ecoDLME>
                                              <ecoRecycle>false</ecoRecycle>
                                              <ecoSavesWater>false</ecoSavesWater>
                                              <ecoHealthyHomes>false</ecoHealthyHomes>
                                              <ecoNurtureNature>false</ecoNurtureNature>
                                              <ecoSavesEnergy>false</ecoSavesEnergy>
                                    </skuNavAttributes>
                          </sku>
                </product>

我只想获取产品的主url,我不关心xml结构中的其他url,比如imageUrl和thumbnailUrl。

我试过了:

sed -rn '/<url>([^"]*)<\/url>/' file.xml > file.txt

然而到目前为止是空的输出。

【问题讨论】:

  • 你试过grep吗?效果如何?
  • 我已经尝试过使用 sed 但到目前为止为空输出。我认为我的正则表达式技能不能胜任这份工作。我添加了一个我现在尝试过的示例。

标签: xml linux awk sed grep


【解决方案1】:

您可以先 grep 查找 &lt;url&gt; 行(如果 XML 文件的格式与您显示的一样),最后删除 XML 标记:

grep '<url>' file.xml | sed 's/.*>\([^<]*\)<.*/\1/' >> file.txt

您可以完全删除标签

grep '<url>' a.txt | sed 's/<\/*url>//g'

&lt;&gt;替换为空格后可以选择第二列:

grep '<url>' a.txt | tr '<>' ' ' | awk '{print $2}'

此外,您可以使用xpath 来选择正确的标签,而不是使用 grep,例如像这样

xpath -q -e '//product/url' file.xml | ... > file.txt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 2012-07-15
    • 2017-04-23
    • 2012-07-11
    • 2021-06-19
    • 2018-08-23
    • 2020-01-17
    相关资源
    最近更新 更多