在Linux中,使用sed、awk、cat或grep将URL从xml传输到单独的文件中

在Linux中,使用sed、awk、cat或grep将URL从xml传输到单独的文件中,xml,linux,awk,sed,grep,Xml,Linux,Awk,Sed,Grep,根据下面的xml示例,我有一个包含许多产品的xml文件 我想grep出这个文档中的所有URL,并将它们导入到一个新文档中。例如,我希望在以下两者之间获取url: <url></url> 以下是xml的一个示例,对于许多产品,这会重复多次: <product> <id>13128613</id> <name>Cooke &

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

我想grep出这个文档中的所有URL,并将它们导入到一个新文档中。例如,我希望在以下两者之间获取url:

<url></url>
以下是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>

13128613
库克;刘易斯宝石三叠系工作台面3050mm
9372151
边缘1长,2短,无需模板减少安装复杂性、时间和成本,这一惊人的设计由85%的回收材料制成,包括玻璃和外壳,支持环境可持续性,6毫米固体材料与28毫米固体刨花板芯粘合,背衬防潮平衡纸,完全防水,表面坚硬,耐日常磨损
http://www.example.com/nav/rooms/kitchens/kitchen-worktops/gemstone_solid_surface_worktops/-specificproducttype-worktops/Cooke-and-Lewis-Gemstone-Triassic-Worktop-3050mm-13128613
房间厨房厨房台面宝石实心表面台面台面台面
13619319
库克;刘易斯宝石三叠系工作台面3050mm
6毫米固体材料与28毫米高性能刨花板芯粘合,Cooke&;刘易斯宝石是完美的绿色选择,配方中含有85%的可回收材料。
5397007119039
新的
582
0
通常在5周内交货
1.
0
1.
只送货上门
厨房
固体表面工作台
SPEEDSTONE实体表面
家居及;花园
库克;刘易斯宝石
http://example.com/is/image/5397007119039_001c_v001_zp
http://example.com/is/image/5397007119039_001c_v001_zp?75x75美元$=
假的
假的
假的
假的
假的
假的
假的
我只想获取产品的主url,而不关心xml结构中的其他url,如imageUrl和thumbnailUrl

我试过:

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

但是,到目前为止输出为空。

您可以首先grep查看
行(如果XML文件的格式与您所示的相同),最后删除XML标记:

grep '<url>' file.xml | sed 's/.*>\([^<]*\)<.*/\1/' >> file.txt
此外,您可以使用
xpath
来选择合适的标记,而不是使用grep,例如

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

你试过grep吗?它是如何工作的?我试过sed,但到目前为止输出是空的。我认为我的正则表达式技能不能胜任这项工作。我已经添加了一个我现在尝试过的例子。
grep '<url>' a.txt | sed 's/<\/*url>//g'
grep '<url>' a.txt | tr '<>' ' ' | awk '{print $2}'
xpath -q -e '//product/url' file.xml | ... > file.txt