substitute the every nth of line, sed

I want to convert these three lines into JSON:
"https://catonrug.blogspot.com/2018/04/add-swap-file-centos-7-amazon-aws.html"
"Add swap file to CentOS 7, Amazon AWS"
"tag:blogger.com,1999:blog-3325322079154148081.post-7205353922662111389"
"https://catonrug.blogspot.com/2017/05/remove-instance-from-aws-ec2.html"
"Remove instance from AWS EC2"
"tag:blogger.com,1999:blog-3325322079154148081.post-4805867199201908752"
originally from:
curl -s "https://catonrug.blogspot.com/feeds/posts/default/-/AWS?alt=json-in-script" |\
sed "s/gdata.io.handleScriptLoaded(//;s/);$//" |\
jq '.["feed"] | .["entry"] | .[] | (.link[] | select(.rel == "alternate").href),(.link[] | select(.rel == "alternate").title),.id."$t"'
The destination should look like:
{
 "data": [{
  "{#URL}": "https://catonrug.blogspot.com/2018/04/add-swap-file-centos-7-amazon-aws.html",
  "{#TITLE}": "Add swap file to CentOS 7, Amazon AWS",
  "{#POST}": "tag:blogger.com,1999:blog-3325322079154148081.post-7205353922662111389"
 }, {
  "{#URL}": "https://catonrug.blogspot.com/2017/05/remove-instance-from-aws-ec2.html",
  "{#TITLE}": "Remove instance from AWS EC2",
  "{#POST}": "tag:blogger.com,1999:blog-3325322079154148081.post-4805867199201908752"
 }]
}
Here is how I implemented it. New lines matter in this script:
curl -s "https://catonrug.blogspot.com/feeds/posts/default/-/AWS?alt=json-in-script" |\
sed "s/gdata.io.handleScriptLoaded(//;s/);$//" |\
jq '.["feed"] | .["entry"] | .[] | (.link[] | select(.rel == "alternate").href),(.link[] | select(.rel == "alternate").title),.id."$t"' |\
sed ': loop;
i {\"{#URL}\":
a ,
n;
i \"{#TITLE}\":
a ,
n;
i \"{#POST}\":
a },
n;
b loop' |\
tr -cd "[:print:]" |\
sed "s/[ \t]*$//" |\
sed "s/^/{\"data\":[/;s/,$/]}/" |\
jq .

No comments: