Change a certain line in several documents with command/variables
-
Good evening together,
i have multiple .json files where i want to replace a certain line in each.
Line 6 in each file contains following content:
1.json (“edition”: 1,)
2.json (“edition”: 2,)
3.json (“edition”: 3,)
4.json (“edition”: 4,)
… etc.
1000.json (“edition”: 1000,)change for each document the line 6 to:
|
|
|
V
1.json ( “external_url” : “https://example.com/?token_id=1”,)
2.json ( “external_url” : “https://example.com/?token_id=2”,)
3.json ( “external_url” : “https://example.com/?token_id=3”,)
4.json ( “external_url” : “https://example.com/?token_id=4”,)
… etc.
1000.json ( “external_url” : “https://example.com/?token_id=1000”,)
Is there any command to execute to change this line for each document?
Many thanks in advance.
-
@ivan-k-0 said in Change a certain line in several documents with command/variables:
Is there any command to execute to change this line for each document?
It’s called regular expression search and replace.
Your method of describing your data is rather confusing. I don’t know whether those digits really prefix each line, or if you are just numbering the lines, or numbering the documents that contain that as line#6. Using the Template for Search/Replace questions mentioned below would have helped you communicate more clearly.
But I can come up with a solution that is agnostic of that prefix anyway.
If you want to replace any
json ("edition": ###,)line withjson ( "external_url":"https://example.com/?token_id=###",)then you can use
FIND =json \("edition": (\d+),\)
REPLACE =json \( "external_url":"https://example.com/?token_id=${1}",\)
SEARCH MODE = Regular ExpressionIf you have many such lines in the same file, but only want to replace the one on line 6, then change the FIND to
\A(?-s:^.*$\R){5}\K^json \("edition": (\d+),\), which looks for the beginning of the file and 5 lines, then restarts the match.If you allow other characters before the
jsonprefix on line 6, you might have to tweak what comes after the\Kto allow that. I will leave that as an exercise for the reader; give it a try using the docs below, and if you cannot figure it out, ask for hints.-—
Useful References
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login