Community
    • Login

    First line replacement

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    6 Posts 4 Posters 2.6k 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.
    • GS MusicG
      GS Music
      last edited by

      Hello everyone,

      I have around 1400 txt files. What need is to place a code on the first line of each file like below.

      Example:
      Before: Title random text
      After: {title: Title random text}

      Is there a way to do this using Notepad++?

      1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones
        last edited by

        So, your first line of the file is always what you are considering “title random text”? This might work for you. (Make sure you save a backup of the files, or set the filter to just one file before running a regex on 1400 files)

        Find in Files

        • Find what: (?-s)\A^.*$
        • Replace with: {title: $0}
        • setup Filters and Directory appropriately
        • Hit Replace in Files.

        Note: I assumed the { and } in your text were literals.

        Explanation:

        • (?-s) turns off dot-matches-newline, overriding the state of the checkbox
        • \A^ is an idiom that matches the beginning of only the first line in the file
        • .*$ matches the entirety of the line (excluding line endings)
        • the whole line’s contents are stored in the $0 complete-match variable
        • the replacement uses the literal {title: , followed by the complete-match variable contents, followed by literal }

        This seems to match what you described, based on my interpretation.

        ----

        Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as plain text using the </> toolbar button or manual Markdown syntax; screenshots can be pasted from the clipbpard to your post using Ctrl+V. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get… Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries.

        1 Reply Last reply Reply Quote 5
        • guy038G
          guy038
          last edited by

          Hello, @gs-music, @peterjones and All,

          In addition, to the regex S/R, proposed by Peter, here are two regex S/R to use IF  your files may contain true empty lines at their very beginning !

          • A) You want to keep possible true empty lines, at very beginning of files and modify the first non-empty line of each file, as expected :

            • SEARCH (?-s)\A^(^\R)*\K.+

            • REPLACE {title:\x20$0}

          • B You want to delete possible true empty lines, at very beginning of files and modify the first non-empty line of each file, as expected :

            • SEARCH (?-s)\A^(^\R)*(.+)

            • REPLACE {title:\x20\2}


          Notes :

          • The first part of regexes (?-s) is an in-line modifier, which forces the regex engine to consider that a dot symbol ( . ) matches a single standard character, only ( and not any EOL char )

          • For explanation of the idiom \A^, refer to this topic : Add line of text to beginning of multiple files and read all posts, preferably from this one :

          https://community.notepad-plus-plus.org/post/50951

          Best Regards

          guy038

          Alan KilbornA 1 Reply Last reply Reply Quote 4
          • Alan KilbornA
            Alan Kilborn @guy038
            last edited by

            @guy038

            When I click on your link with URL text of:

            https://community.notepad-plus-plus.org/post/50951

            it actually takes me here:

            https://community.notepad-plus-plus.org/topic/11987/add-line-of-text-to-beginning-of-multiple-files/11

            PeterJonesP 1 Reply Last reply Reply Quote 0
            • PeterJonesP
              PeterJones @Alan Kilborn
              last edited by

              @Alan-Kilborn said in First line replacement:

              When I click on your link with URL text of:
              https://community.notepad-plus-plus.org/post/50951
              it actually takes me here:

              Yes. The new forum has both “topic” (which numbers the replies as a subportion of the the URL) and “post”, which links directly to the unique database id for a given post – whether it be the initial topic or a posted reply. If you right click on the datestamp for the post https://community.notepad-plus-plus.org/topic/11987/add-line-of-text-to-beginning-of-multiple-files/11, it shows the URL https://community.notepad-plus-plus.org/post/50951 – they refer to the same reply. Under the hood, the forum redirects from the post/#### to the topic/######/___name___/##.

              1 Reply Last reply Reply Quote 3
              • GS MusicG
                GS Music
                last edited by

                Thanks guys. Both solutions worked fine. Thank you for your help.

                1 Reply Last reply Reply Quote 1
                • First post
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors