printing each element of an array with awk

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 --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: 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: 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: width:0 height:0 images:1:item_id:1269584860 image_id:1 src: width:0 height:0 credit: caption: 2:item_id:1269584860 image_id:2 src: width:0 height:0 credit: caption: listen_duration_estimate:290 401589572:item_id:401589572 resolved_id:401589572 given_url: 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: 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: twrp-or-cwm-recovery-without-flashing/amp/ top_image_url: listen_duration_estimate:91 item_id:1040637704 resolved_id:1040637704 given_url: 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: 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: top_image_url: Recovery.png authors:36373373:item_id:1040637704 author_id:36373373 name:Androiding Staff url: listen_duration_estimate:104 
4

1 Answer

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.

0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like