使用sed,awk,cat或grep将xml中的xl管道分配到Linux中的单独文件中

时间:2014-06-06 15:59:38

标签: xml linux awk sed grep

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

我想要查看本文档中的所有网址并将它们传输到新文档中。例如,我想获取之间的URL:

<url></url>

并将这些文件传输到新的txt文件中,每个网址都在新行上。所以输出看起来像是一个网址列表:

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>

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

我试过了:

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

然而到目前为止空输出。

1 个答案:

答案 0 :(得分:0)

您可以首先grep <url>行(如果XML文件的格式与您一样),最后删除XML标记:

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

您可以完全删除标签

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

您可以在用空格替换<>之后选择第二列:

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

另外,您可以使用xpath选择正确的标记,而不是使用grep。像这样

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