繁体   English   中英

用awk打印数组的每个元素

[英]printing each element of an array with awk

我正在尝试使用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

使用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!"

您的属性数组现在无用了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM