printing each element of an array with awk

1.7k views Asked by At

I am trying to backup my Pocket bookmarks to csv & html using the API and I'm having trouble printing each element of an array without blank lines or specifically referencing each element.

The consumer key & access token have been replaced with * in the curl command.

#!/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"

The output looks like this with lots of blank lines because not all of the elements exist:

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
1

There are 1 answers

0
glenn jackman On

Use the return value of the split function:

split() returns the number of elements created.

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

But, don't use awk to parse JSON. Use a JSON parser. Imagine the value of some attribute contains a comma:

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

Your attribute array is now useless.