• Login
Community
  • Login

Change a certain line in several documents with command/variables

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
2 Posts 2 Posters 634 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I
    Ivan K 0
    last edited by Ivan K 0 Apr 5, 2022, 4:38 PM Apr 5, 2022, 4:38 PM

    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.

    P 1 Reply Last reply Apr 5, 2022, 4:57 PM Reply Quote 0
    • P
      PeterJones @Ivan K 0
      last edited by PeterJones Apr 5, 2022, 4:57 PM Apr 5, 2022, 4:57 PM

      @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 with json ( "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 Expression

      If 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 json prefix on line 6, you might have to tweak what comes after the \K to 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

      • Please Read Before Posting
      • Template for Search/Replace Questions
      • FAQ: Where to find regular expressions (regex) documentation
      • Notepad++ Online User Manual: Searching/Regex
      1 Reply Last reply Reply Quote 3
      2 out of 2
      • First post
        2/2
        Last post
      The Community of users of the Notepad++ text editor.
      Powered by NodeBB | Contributors