Community
    • Login

    regex to move entire line in a specific line position in multiples file

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    14 Posts 3 Posters 803 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.
    • Rockberto ManentiR
      Rockberto Manenti @Alan Kilborn
      last edited by

      @Alan-Kilborn
      Thanx again. I’m definitely doing something wrong and it doesn’t work.
      I simplify the example and remove " in the files

      let’s say I have some text files composed like this

      DATE 1980
      GENRE HEAVY METAL
      PERFORMER SAXON
      TITLE WHEELS OF STEEL

      and another like this

      GENRE HARD ROCK
      PERFORMER LED ZEPPELIN
      TITLE PRESENCE
      DATE 1976

      I would like to have these results

      PERFORMER SAXON
      TITLE WHEELS OF STEEL
      DATE 1980
      GENRE HEAVY METAL

      PERFORMER LED ZEPPELIN
      TITLE PRESENCE
      DATE 1976
      GENRE HARD ROCK

      thanks again for your patience

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

        @Alan-Kilborn ,

        With the original data

        REM DATE XXXX
        REM GENRE "XXXX"
        PERFORMER "XXXXXXXX"
        TITLE "XXXXXXXX"
        

        using your regex, I got 0 matches when REM DATE XXXX is the first line in the file.

        If I put any lines before that, like:

        any line before
        REM DATE XXXX
        REM GENRE "XXXX"
        PERFORMER "XXXXXXXX"
        TITLE "XXXXXXXX"
        

        … then your regex matches and gives the expected results:

        PERFORMER "XXXXXXXX"
        TITLE "XXXXXXXX"
        REM DATE XXXX
        REM GENRE "XXXX"
        any line before
        

        If I change from .+? to .*?, so my full regex is (?s-i)\A(.*?)(REM DATE \w+\RREM GENRE "\w+"\R)(PERFORMER "\w+"\RTITLE "\w+"\R) then I get

        PERFORMER "XXXXXXXX"
        TITLE "XXXXXXXX"
        REM DATE XXXX
        REM GENRE "XXXX"
        

        As my output.

        So that’s what I’d recommend to @Rockberto-Manenti for the original problem statement.

        @Rockberto-Manenti used a different set of data the second time:

        DATE 1980
        GENRE HEAVY METAL
        PERFORMER SAXON
        TITLE WHEELS OF STEEL
        

        With those longer pieces of text, it also comes to light that even with prefix lines, Alan’s wouldn’t have matched, because he assumed exactly one word (no spaces) inside the quotes. But since you show spaces are possible in the XXXX sequence, and I am guessing that somewhere you also have punctuation (since performers and song titles sometimes do), I would recommend (?s-i)\A(.*?)(?-s)(REM DATE .*?\RREM GENRE ".*?"\R)(PERFORMER ".*?"\RTITLE ".*?"\R) if you have quotes and (?s-i)\A(.*?)(?-s)(DATE .*?\RGENRE .*?\R)(PERFORMER .*?\RTITLE .*?\R) if you have neither quote marks nor REM prefix.

        Please note: changing your spec (like having REM before it originally and then changing it to not, or having quotes around the words or not) will change the regex. Showing us real data originally, instead of XXXX data, would have prevented some of the confusion.

        And I’m not convinced that you’ve actually shown things with enough variety yet. I am guessing that you might really have those four lines in any of the 4!=24 possible orders, and you always want them to output in the order of PERFORMER, TITLE, DATE, GENRE, regardless of order. This would completely change the regex again (pretty drastically) to be able to handle all possible orders.

        For example, assuming that the first four lines are always one of the DATE, GENRE, PERFORMER, and TITLE lines, with anything after on the same line, then I would use (?=(?s:.*)(?-s:(^PERFORMER\b.*\R)))(?=(?s:.*)(?-s:(^TITLE\b.*\R)))(?=(?s:.*)(?-s:(^DATE\b.*\R)))(?=(?s:.*)(?-s:(^GENRE\b.*\R)))(?-s:^.+?\R){4,} replaced with ${1}${2}${3}${4} . Though mine won’t work if you have the any other data lines before those four lines, or in between those lines.

        This is why our Template for Search/Replace Questions specifically says that you should make the data match as close to your real-world problem as you can. If you had used that template, you would have saved yourself and Alan lots of time.

        I can understand using XXXXX when you have secret data (real names, addresses, business info, etc). But honestly, for a list of songs and their performers, there’s nothing secret, and all your XXXing did was waste a bunch of time and effort.

        ----

        Useful References

        • Please Read Before Posting
        • Template for Search/Replace Questions
        • Formatting Forum Posts
        • Notepad++ Online User Manual: Searching/Regex
        • FAQ: Where to find other regular expressions (regex) documentation
        Alan KilbornA 1 Reply Last reply Reply Quote 1
        • Alan KilbornA
          Alan Kilborn @PeterJones
          last edited by Alan Kilborn

          @PeterJones said in regex to move entire line in a specific line position in multiples file:

          using your regex, I got 0 matches when REM DATE XXXX is the first line in the file.

          So when people don’t ask their questions correctly, (a) I should move on to reading the next post – which I should have done in this case, or (b) sometimes I copy their whole original post into Notepad++ and start working with that as their data – which I did in this case.

          Often, as people respond and the back and forth grows lengthy, I mainly lose interest, but sometimes their need is clarified, making the original post read differently.

          So…in this case, if you copy the OP’s first post contents into Notepad++, and fixup the quotes to be normal quotes, and then run my regex replacement on it, you’ll see that it does what was originally requested (or at least how I interpreted the need the first time I read it).

          Really, I should choose my option (a) more often.


          and all your XXXing did was waste a bunch of time and effort.

          So true, users think they know how to generalize a question; they’d be better off providing a real example the first time around.

          1 Reply Last reply Reply Quote 1
          • Rockberto ManentiR
            Rockberto Manenti
            last edited by

            I deeply apologize for the confusion. My fault especially for my incorrect English.
            In the first example I had put XXXX only to indicate generic words.
            There is no hidden data, they are all .cue text files (for flac music) in which however the order of the four fields (PERFORMER, TITLE, DATE, GENRE) is not always the same, they can often be in different positions.
            On notepad++ I created a macro that eliminates everything superfluous and leaves me with only the four lines in question, but for cataloging purposes, I would like all the .cue files to have the same order, therefore the entire line that begins with PERFORMER be the first, the line starting with TITLE is the second, the line starting with DATE is the third and the line starting with GENRE is the last.
            Thanks again and I apologize again

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

              @Rockberto-Manenti said in regex to move entire line in a specific line position in multiples file:

              I created a macro that eliminates everything superfluous and leaves me with only the four lines in question

              Then after runnnig that macro, add the regex I supplied, because what I supplied will take those four lines in any order and replace the with the four lines in the order you requested.

              1 Reply Last reply Reply Quote 0
              • Rockberto ManentiR
                Rockberto Manenti
                last edited by

                sorry once again for the inconvenience, but I tried to use the regex but I get no results.
                the message says nothing has changed, What am I doing wrong?

                Alan KilbornA 1 Reply Last reply Reply Quote 0
                • Alan KilbornA
                  Alan Kilborn @Rockberto Manenti
                  last edited by

                  @Rockberto-Manenti said in regex to move entire line in a specific line position in multiples file:

                  What am I doing wrong?

                  Hard to say without more information from you.

                  If I take one of your original blocks of text and do the Replace on it with Peter’s expression:

                  99079a79-b68c-4b2f-9d81-8905981b30f3-image.png

                  Then I obtain the ordering that I think you want:

                  e17f3206-7156-419a-be0b-83255293dd0a-image.png

                  1 Reply Last reply Reply Quote 1
                  • Rockberto ManentiR
                    Rockberto Manenti
                    last edited by

                    I don’t know the reason but this is the result

                    prova.jpg

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

                      @Rockberto-Manenti said in regex to move entire line in a specific line position in multiples file:

                      I don’t know the reason but this is the result

                      It’s because your file doesn’t end in a newline.

                      The easiest way to fix that is to change your macro that you use before this to make sure there’s a newline (\r\n) at the end of the last line

                      The other option would be every time you see a \R in the regex that I supplied, use (?:\R|\Z) instead.

                      Rockberto ManentiR 1 Reply Last reply Reply Quote 0
                      • Rockberto ManentiR
                        Rockberto Manenti @PeterJones
                        last edited by

                        @PeterJones said in regex to move entire line in a specific line position in multiples file:

                        It’s because your file doesn’t end in a newline.

                        Genius!!! Works great!!!
                        many many many thanx!!! you rulez!

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