0

我正在尝试使用 API 将我的 Pocket 书签备份到 csv 和 html,并且在打印没有空行或专门引用每个元素的数组的每个元素时遇到问题。

在 curl 命令中,消费者密钥和访问令牌已替换为 *。

#!/bin/bash

curl https://getpocket.com/v3/get --insecure -X POST -H "Content-Type: application/json" -H "X-Accept: application/json" -d "{\"consumer_key\":\"*\", \"access_token\":\"*\", \"detailType\":\"complete\"}" > "pocketData"

awk 'BEGIN {
        FS="\"[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\":" 
    }
    {
        for (i = 2; i <= NF; i++)           
        {
            count++
            gsub("\{", "", $i)
            gsub("\}", "", $i)
            gsub("\\", "", $i)
            gsub("\"", "", $i)
            gsub("\\n", "", $i)
            split($i, attributeArray, ",")


            print attributeArray[1]
            print attributeArray[2]
            print attributeArray[3]
            print attributeArray[4]
            print attributeArray[5]
            print attributeArray[6]
            print attributeArray[7]
            print attributeArray[8]
            print attributeArray[9]
            print attributeArray[10]
        #...
            print attributeArray[100]
            #print

        }
    }' "pocketData"

输出看起来像这样,有很多空行,因为并非所有元素都存在:

item_id:1269584860
resolved_id:1269584860
given_url:http://www.marshmallow-news.xyz/2015/12/lg-k7-7848.html
given_title:
favorite:0
status:0
time_added:1461709832
time_updated:1461709836
time_read:0
time_favorited:0
sort_id:434
resolved_title:Upgrade/Update LG K7 to 6.0 Marshmallow
resolved_url:http://www.marshmallow-news.xyz/2015/12/lg-k7-7848.html
excerpt:Good news to everyone! LG K7 owners can now update their mobile 
phone's operating system Android 6.0 Marshmallow by using custom ROM. G 
oogle's newest OS
the Android 6.0 Marshmallow
comes in with many improvements and notable features such as:
is_article:1
is_index:0
has_video:0
has_image:1
word_count:748
lang:en
time_to_read:3
image:item_id:1269584860
src:http://60marshmallow.com/1.jpg
width:0
height:0
images:1:item_id:1269584860
image_id:1
src:http://60marshmallow.com/1.jpg
width:0
height:0
credit:
caption:
2:item_id:1269584860
image_id:2
src:http://60marshmallow.com/2.jpg
width:0
height:0
credit:
caption:
listen_duration_estimate:290
401589572:item_id:401589572
resolved_id:401589572
given_url:https://aubykhan.wordpress.com/2013/07/21/android-tip-boot-into- 
twrp-or-cwm-recovery-without-flashing/
given_title:
favorite:0
status:0
time_added:1461709224
time_updated:1461709224
time_read:0
time_favorited:0
sort_id:435
resolved_title:Android Tip: Boot into twrp or cwm recovery without flashing
resolved_url:https://aubykhan.wordpress.com/2013/07/21/android-tip-boot- 
into-twrp-or-cwm-recovery-without-flashing/
excerpt:Yes
thatu2019s true. You can boot into any recovery of your choice on your 
Android phone by following this guide. No need to flash and replace the 
stock bootloader. Tho
ugh I have tested this method on Nexus devices only but I am pretty sure 
that itu2019ll work on other phones as well.
is_article:1
is_index:0
has_video:0
has_image:0
word_count:235
lang:en
amp_url:https://aubykhan.wordpress.com/2013/07/21/android-tip-boot-into- 
twrp-or-cwm-recovery-without-flashing/amp/
top_image_url:https://s0.wp.com/i/blank.jpg
listen_duration_estimate:91


































item_id:1040637704
resolved_id:1040637704
given_url:http://androiding.how/how-to-install-twrp-recovery-via-fastboot/
given_title:
favorite:0
status:0
time_added:1461709207
time_updated:1461709207
time_read:0
time_favorited:0
sort_id:436
resolved_title:How to Install TWRP Recovery via Fastboot
resolved_url:http://androiding.how/how-to-install-twrp-recovery-via- 
fastboot/
excerpt:Bootloader / Fastboot mode allows you to flash any partition on a 
device
be it system
boot
recovery
cache.. anything. Andu00a0not just the partition images from OEMs
you can also flash theu00a0custom built .img files viau00a0fastboot. For 
example
a custom recovery like TWRP.
is_article:1
is_index:0
has_video:0
has_image:0
word_count:269
lang:en
amp_url:http://androiding.how/how-to-install-twrp-recovery-via-fastboot/amp/
top_image_url:http://androiding.how/wp-content/uploads/2015/07/TWRP- 
Recovery.png
authors:36373373:item_id:1040637704
author_id:36373373
name:Androiding Staff
url:http://androiding.how/author/shivam/
listen_duration_estimate:104
4

1 回答 1

0

使用split函数的返回值:

split()返回创建的元素数。

        n = split($i, attributeArray, ",")
        for (i = 1; i <= n; i++) print attributeArray[i]

但是,不要使用 awk 来解析 JSON。使用 JSON 解析器。想象一下某个属性的值包含一个逗号:

"excerpt":"Good news to everyone, except you!"

你的属性数组现在没用了。

于 2018-11-12T18:06:03.843 回答