Help a bash brainlet out plz

I'll try to keep it brief.

I have a folder that has one .txt file that contains a list of urls (one per line)

Also in this folder, I have multiple .json files. The .json files reads like this:

{

"dna": "eebfeb854ea77e6c6eb0f2b960575f87",

"name": "Untitled Project #1200",

"description": null,

"image": "REPLACE-THIS-WITH-YOUR-URL/1200.png",

"date": 1646259609387,

"attributes": [
{}
}

I want to replace the "REPLACE-THIS-WITH-YOUR-URL/1200.png" with one url from the list. Also I want to do the script so that only it gives the first URL to the first .json file in the map, only the second url to the second .json file in the map and so on.

The way I tried to do it (which did not work) was like this:

#! /bin/bash

urls=urls.txt

i=0

until [ $i -gt 378 ]

do

sed -i "s/REPLACE-THIS-WITH-YOUR-URL/.*/"${urls[i]}"/g" *

((i=i+1))

done

There are 379 .json files in the folder, hense the -gt 378! Any help would be gladly appreciated. Thank you in advance

Attached: ill-be-bash-yavn8u.jpg (600x345, 17.57K)

Other urls found in this thread:

stackoverflow.com/a/61049639
stackoverflow.com/a/30988704
twitter.com/AnonBabble

Just use jq

You need to learn how to use code blocks when posting code. I'll clean this up for you. {
"dna": "eebfeb854ea77e6c6eb0f2b960575f87",
"name": "Untitled Project #1200",
"description": null,
"image": "REPLACE-THIS-WITH-YOUR-URL/1200.png",
"date": 1646259609387,
"attributes": [ {} ]
}


#! /bin/bash
urls=urls.txt
i=0
until [ $i -gt 378 ]
do
sed -i "s/REPLACE-THIS-WITH-YOUR-URL/.*/"${urls[i]}"/g" *
((i=i+1))
done


That's not going to work, and I feel like you're making potentially unsafe assumptions about the order your script will iterate through the JSON files in the directory.

import json

you're retarded and I could do this in a minute, but I'm phoneposting so I won't

Sorry, haha will learn

How would you do it?

Use jq and think about your problem a little bit more.
stackoverflow.com/a/61049639

Well I'm still new to bash, so. A solution or explanation would be much appreciated

The `sed REGEXP *` shows a lack of understanding. That would apply the regexp on all the files instead of one file at a time. You gotta think.

How would you do it then?

How are your json files named? Are you certain of their ordering?

Sounds like a homework problem.

Also, is the URL "REPLACE-THIS-WITH-YOUR-URL/1200.png" inside each json file or is it different?

First, you have to define a sort order for your JSON files if you want to be correct? What does it even mean to be the first JSON file?
Then you need to iterate through them one by one.
If it's just ASCII order, you can say something like:

for j in $(ls *.json) ; do
# do something with jq like stackoverflow.com/a/61049639
done


You might want to look into bash arrays so that you can load the contents of the text file with URLs into it.

It does. Figure out the rest yourself.

they are named from 1200.json and up, and the /1200.png is matching with the name of the file, so everyone is unique

Thank you. they are all named numbers (from 1200 and up) so what i ment was the lowest number. Ill check it out

>Putting the contents of a text file into a bash array:
stackoverflow.com/a/30988704

#!/bin/bash

URLS+=(`cat url.txt`)
i=0

while [ $i -lt 378 ]
do

sed -i "s/REPLACE-THIS-WITH-YOUR-URL\/.*/"${URLS[i]}"/g" $i.json
((i++))
done


hope this helps somewhat. I assumed that there's some correlation between the name of the file and the url you want to replace.

cat urls.txt | for file in *.json; do
read url && sed -i "s REPLACE-THIS-WITH-YOUR-URL $url " "$file"
done

I didn't want to just do it for you but I don't like

this is a great solution, better than mine. However you are relying on shell globbing to get the files which are not sorted, rather then just *.json use ls *.json or use the sort tool