【问题标题】:Extract text prefix and url list to consequtive numbered output line提取文本前缀和 url 列表到连续编号的输出行
【发布时间】:2019-10-08 08:14:15
【问题描述】:

我正在尝试从包含 url 列表的文本文件 (raw.txt) 中提取“product_sku”。然后我需要将提取的文本作为后缀添加到每个编号为 (0-36) 的 url 的末尾到输出文件 (clean.txt)

每组 url 都有不同的“product_sku”,所以每组行都需要对应 url 组的“product_sku”。

我可以使用以下方法提取网址:

cat raw.txt | grep -E -o "WEBSITE/product_images/870-.*" > clean.txt

我正在努力将“product_sku”添加到列表中。

代码如下所示。

... ... 表示为清楚起见已删除的其他 url。

var StockNumber = 'U52684'
var product_id = '972930'
var product_sku = 'NN26ZZA'
        var threesixty_start_frame = 4
    var threesixty_images = WEBSITE/product_images/870-972930/XafJtPxTqNQuPNQpjF5h.JPG
WEBSITE/product_images/870-972930/9YZH6VMT2zH6AntfaAIG.JPG
WEBSITE/product_images/870-972930/SrE9oVwVmJjS58SwZB9q.JPG
WEBSITE/product_images/870-972930/1enLX6dc9bbG7k9SEAfL.JPG

... ...

var StockNumber = 'U75102'
var product_id = '768442'
var product_sku = 'LT65YMP'
        var threesixty_start_frame = 4
    var threesixty_images = WEBSITE/product_images/870-768442/fg7G7CX2Z0oK8mCVlJN3.JPG
WEBSITE/product_images/870-768442/PGGJ5RaWoYx7VStbsBjL.JPG
WEBSITE/product_images/870-768442/SEEbiNEWA5MXsIUwuaZH.JPG
WEBSITE/product_images/870-768442/TrEPAFaEhxhMxQNDD8qh.JPG

... ...

var StockNumber = 'U79649'
var product_id = '957033'
var product_sku = 'LS16ZBC'
        var threesixty_start_frame = 4
    var threesixty_images = WEBSITE/product_images/870-957033/K5iXEYmG2a4QncRBOrvL.JPG
WEBSITE/product_images/870-957033/fbaj3T5dKtmH0HTX11q5.JPG
WEBSITE/product_images/870-957033/WvBUOrjCMWQGe4gwNhrF.JPG
WEBSITE/product_images/870-957033/ixtB4SbtrFZTIVotvxSd.JPG

... ...

理想的输出应该是这样的:

WEBSITE/product_images/870-972930/XafJtPxTqNQuPNQpjF5h.JPG NN26ZZA_01.JPG
WEBSITE/product_images/870-972930/9YZH6VMT2zH6AntfaAIG.JPG NN26ZZA_02.JPG
WEBSITE/product_images/870-972930/SrE9oVwVmJjS58SwZB9q.JPG NN26ZZA_03.JPG
WEBSITE/product_images/870-972930/1enLX6dc9bbG7k9SEAfL.JPG NN26ZZA_04.JPG
...
...
WEBSITE/product_images/870-768442/fg7G7CX2Z0oK8mCVlJN3.JPG LT65YMP_01.JPG
WEBSITE/product_images/870-768442/PGGJ5RaWoYx7VStbsBjL.JPG LT65YMP_02.JPG
WEBSITE/product_images/870-768442/SEEbiNEWA5MXsIUwuaZH.JPG LT65YMP_03.JPG
WEBSITE/product_images/870-768442/TrEPAFaEhxhMxQNDD8qh.JPG LT65YMP_04.JPG
...
...

等等。等等

【问题讨论】:

    标签: curl awk sed grep


    【解决方案1】:

    您可以使用awk 捕获上一行中看到的product_sku,并根据要求重复使用它。例如,这里 product_sku 用于所有具有WEBSITE 的行。请注意,每次看到 product_sku 时,c 的计数器也会重置。以便为每个 product_sku 重新编号。

    awk  '/product_sku/{p_sku=$NF; c=1;next} /WEBSITE/{url=gensub(/.*(WEBSITE.*)/,"\\1","g");print url,p_sku "_"c".jpeg";c++}' input
    WEBSITE/product_images/870-972930/XafJtPxTqNQuPNQpjF5h.JPG NN26ZZA_1.jpeg
    WEBSITE/product_images/870-972930/9YZH6VMT2zH6AntfaAIG.JPG NN26ZZA_2.jpeg
    WEBSITE/product_images/870-972930/SrE9oVwVmJjS58SwZB9q.JPG NN26ZZA_3.jpeg
    WEBSITE/product_images/870-972930/1enLX6dc9bbG7k9SEAfL.JPG NN26ZZA_4.jpeg
    WEBSITE/product_images/870-768442/fg7G7CX2Z0oK8mCVlJN3.JPG LT65YMP_1.jpeg
    WEBSITE/product_images/870-768442/PGGJ5RaWoYx7VStbsBjL.JPG LT65YMP_2.jpeg
    WEBSITE/product_images/870-768442/SEEbiNEWA5MXsIUwuaZH.JPG LT65YMP_3.jpeg
    WEBSITE/product_images/870-768442/TrEPAFaEhxhMxQNDD8qh.JPG LT65YMP_4.jpeg
    WEBSITE/product_images/870-957033/K5iXEYmG2a4QncRBOrvL.JPG LS16ZBC_1.jpeg
    WEBSITE/product_images/870-957033/fbaj3T5dKtmH0HTX11q5.JPG LS16ZBC_2.jpeg
    WEBSITE/product_images/870-957033/WvBUOrjCMWQGe4gwNhrF.JPG LS16ZBC_3.jpeg
    WEBSITE/product_images/870-957033/ixtB4SbtrFZTIVotvxSd.JPG LS16ZBC_4.jpeg
    

    注意:此解决方案需要 gawk 才能运行。

    【讨论】:

    • @PS。正如OP指出的那样,您可能需要在p_sku=$NF; 之前添加gsub("\047", "", $NF); 以删除单引号。
    • @Demartini 将上述命令中的打印语句更改为printf "%s %s%s%02d%s\n", url, p_sku, "_", c, ".jpeg";
    【解决方案2】:
    $ cat tst.awk
    $2 == "product_sku" {
        sku = $4
        gsub(/\047/,"",sku)
        cnt = 0
    }
    s = index($0,"WEBSITE/product_images/870-") {
        printf "%s %s_%02d.JPG\n", substr($0,s), sku, ++cnt
    }
    
    $ awk -f tst.awk file
    WEBSITE/product_images/870-972930/XafJtPxTqNQuPNQpjF5h.JPG NN26ZZA_01.JPG
    WEBSITE/product_images/870-972930/9YZH6VMT2zH6AntfaAIG.JPG NN26ZZA_02.JPG
    WEBSITE/product_images/870-972930/SrE9oVwVmJjS58SwZB9q.JPG NN26ZZA_03.JPG
    WEBSITE/product_images/870-972930/1enLX6dc9bbG7k9SEAfL.JPG NN26ZZA_04.JPG
    WEBSITE/product_images/870-768442/fg7G7CX2Z0oK8mCVlJN3.JPG LT65YMP_01.JPG
    WEBSITE/product_images/870-768442/PGGJ5RaWoYx7VStbsBjL.JPG LT65YMP_02.JPG
    WEBSITE/product_images/870-768442/SEEbiNEWA5MXsIUwuaZH.JPG LT65YMP_03.JPG
    WEBSITE/product_images/870-768442/TrEPAFaEhxhMxQNDD8qh.JPG LT65YMP_04.JPG
    WEBSITE/product_images/870-957033/K5iXEYmG2a4QncRBOrvL.JPG LS16ZBC_01.JPG
    WEBSITE/product_images/870-957033/fbaj3T5dKtmH0HTX11q5.JPG LS16ZBC_02.JPG
    WEBSITE/product_images/870-957033/WvBUOrjCMWQGe4gwNhrF.JPG LS16ZBC_03.JPG
    WEBSITE/product_images/870-957033/ixtB4SbtrFZTIVotvxSd.JPG LS16ZBC_04.JPG
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-02
      相关资源
      最近更新 更多