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
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.
Chase Davis
import json
Camden Brooks
you're retarded and I could do this in a minute, but I'm phoneposting so I won't
Well I'm still new to bash, so. A solution or explanation would be much appreciated
Easton Howard
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.
Asher Smith
How would you do it then?
Cameron Lee
How are your json files named? Are you certain of their ordering?
Jayden Peterson
Sounds like a homework problem.
Brandon Johnson
Also, is the URL "REPLACE-THIS-WITH-YOUR-URL/1200.png" inside each json file or is it different?
Michael Peterson
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:
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.
Gavin Stewart
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
Charles Gonzalez
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