Community
    • Login

    Change a certain line in several documents with command/variables

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    2 Posts 2 Posters 1.3k Views 2 Watching
    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.
    • Ivan K 0I Offline
      Ivan K 0
      last edited by Ivan K 0

      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.

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP Online
        PeterJones @Ivan K 0
        last edited by PeterJones

        @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

        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
        • First post
          Last post
        The Community of users of the Notepad++ text editor.
        Powered by NodeBB | Contributors