2017-06-19 64 views
-1

背景信息: 我已经得到了我的供应商上传每天晚上用新产品和更新的股票数等 一个XML文件,但他们已经缝合了我和他们不在XML文件中没有描述,他们有一个链接到他们的网站,它有原始文本的描述。
查找和URL的内容替换URL

什么,我需要做的是,通过我从他们下载和URL的内容替换URL文件循环的脚本。

举例来说,如果我有

<DescriptionLink>http://www.leadersystems.com.au/DataFeed/ProductDetails/AT-CHARGERSTATION-45</DescriptionLink> 

我希望它最终成为

<DescriptionLink>Astrotek USB Charging Station Charger Hub 3 Port 5V 4A with 1.5m Power Cable White for iPhone Samsung iPad Tablet GPS</DescriptionLink> 

我已经尝试了一些东西,但我不是很精通使用脚本或循环。 到目前为止,我已经有了:

#!/bin/bash 
LINKGET=`awk -F '|' '{ print $2 }' products-daily.txt` 

wget -O products-daily.txt http://www.suppliers-site-url.com 
sed 's/<DescriptionLink>*/<DescriptionLink>$(wget -S -O- $LINKGET/g' products-daily.txt 

但同样,我不知道这一切是如何真正起作用的,所以它是摸着石头过河。 任何帮助表示赞赏!

更新为包含示例URL。

+0

你能提供一个url的例子吗?否则很难测试... –

+0

添加的URL例如:) – Mitchell

回答

0

你会(使用GNU AWK的第三个参数匹配())想是这样的:

$ cat tst.awk 
{ 
    head = "" 
    tail = encode($0) 
    while (match(tail,/^([^{]*[{])([^}]+)(.*)/,a)) { 
     desc = "" 
     cmd = "curl -s \047" a[2] "\047" 
     while ((cmd | getline line) > 0) { 
      desc = (desc=="" ? "" : desc ORS) line 
     } 
     close(cmd) 
     head = head decode(a[1]) desc 
     tail = a[3] 
    } 
    print head decode(tail) 
} 
function encode(str) { 
    gsub(/@/,"@A",str) 
    gsub(/{/,"@B",str) 
    gsub(/}/,"@C",str) 
    gsub(/<DescriptionLink>/,"{",str) 
    gsub(/<\/DescriptionLink>/,"}",str) 
    return str 
} 
function decode(str) { 
    gsub(/}/,"</DescriptionLink>",str) 
    gsub(/{/,"<DescriptionLink>",str) 
    gsub(/@C/,"}",str) 
    gsub(/@B/,"{",str) 
    gsub(/@A/,"@",str) 
    return str 
} 

$ awk -f tst.awk file 
<DescriptionLink>Astrotek USB Charging Station Charger Hub 3 Port 5V 4A with 1.5m Power Cable White for iPhone Samsung iPad Tablet GPS</DescriptionLink> 

https://stackoverflow.com/a/40512703/1745001的信息是什么编码/解码功能正在做的和为什么。

请注意,这是使用getline合适的罕见情况之一。如果你曾经使用考虑getline在未来确保您已阅读并完全理解所有的警告,并使用在http://awk.freeshell.org/AllAboutGetline首先讨论的情况。

+1

谢谢这么多埃德!这看起来已经解决了它!传说! – Mitchell

+0

不客气。现在删除我的意见,整理... –

+0

当在5000+条目我在我的文件中运行这个命令我得到一个错误说''致命的:无法打开管道'卷曲-S(打开的文件太多)'' 任何想法的爱德? – Mitchell