Community
    • Login

    How to find all lines ending with only CR

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    9 Posts 4 Posters 4.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.
    • Bjørn DidriksenB
      Bjørn Didriksen
      last edited by

      Hi,

      I export about 15k of lines every day to crunch in Excel.
      My problem here is that it contains a few carrige returns (CR) without the line feed (LF). And that does not fly…

      I have searched for single LF using ^(.*?|^\r)\n, as these will also f things up :(
      (as described here: https://community.notepad-plus-plus.org/topic/17213/how-to-find-a-line-feed-lf-without-carriage-return-cr-after-every-line)

      I have tried wrapping my head around the correct expression to use to locate the CR only ones, but I must admit that I have not been able to wrap my head around that thing… Any good suggestions? I am tired of scrolling the mouse wheel every morning…

      Alan KilbornA 1 Reply Last reply Reply Quote 0
      • Terry RT
        Terry R
        last edited by

        @Bjørn-Didriksen said in How to find all lines ending with only CR:

        My problem here is that it contains a few carrige returns (CR) without the line feed (LF). And that does not fly…

        It’s interesting that in spite of that old post you refer to you make no mention of the EOL conversion mentioned there. As you don’t mention your intention on finding these incorrect line endings, but do say they cause problems it seems you do want to edit those lines and make the file consistent with line endings. The EOL conversion does that for you.

        The relevant entry in the online manual is located here, then down another couple of screens, or just look for EOL Conversion.

        Terry

        1 Reply Last reply Reply Quote 1
        • Alan KilbornA
          Alan Kilborn @Bjørn Didriksen
          last edited by

          @Bjørn-Didriksen

          In the spirit of answering the exact question asked: “How to find all lines ending with only CR”

          It seems like the regular expression (?-s)^.*?\r(?!\n) should do it.

          1 Reply Last reply Reply Quote 2
          • Terry RT
            Terry R
            last edited by Terry R

            @Bjørn-Didriksen said in How to find all lines ending with only CR:

            Any good suggestions? I am tired of scrolling the mouse wheel every morning…

            My post last night was from a tablet so I was unable to provide a regex I could test first, so thanks to @Alan-Kilborn for providing that.

            When you open a file in Notepad++ (NPP) it recognises the file EOL type from key line(s). In a quick test I had, the first EOL is the CR (Mac) and the bottom status bar of NPP shows that. So in spite of my test file having a mixture of all 3 EOL types it seemed to pick the first one as being representative of the file type.

            So in your case, if you had the file represented as being a “Windows (CR LF)” type, when you check the EOL Conversion menu entry you will see Windows (CR LF) as being dimmed, so not selectable. See my example:

            59a30c83-1d9f-4780-b64d-2dffee0e96d9-EOL1.jpg

            So, as mentioned in the old post you referred to, you firstly need to switch to another EOL type, then back to Windows (CR LF). That will change all your EOLs to Windows (CR LF) as it seems you need to do.

            There is one final test you can do to confirm you have all EOLs as the correct Windows type. Use the Find function and type in \r\n and press count. The number should be 1 less than the number of lines which is also shown in the bottom status bar. If that number doesn’t look right then you can determine the file has a mixture of EOL types. To confirm for yourself that the EOL conversion works, perform the count first, do the conversion and do the count again.

            Terry

            1 Reply Last reply Reply Quote 1
            • Bjørn DidriksenB
              Bjørn Didriksen
              last edited by

              Thank you all :)

              None of these seems to help me fix the problem totally, so I might need to give the entire story on this one…

              Every morning I pull a report from SAP called ZE07 from the system of an oil rig operator. For those not in the know: This report gives me a list off all materials ordered from a work order. It averages about 11-12k of lines. Not to bad, but I have been doing this 5 times a week for close to a year now… My scrolling wheel mouse finger looks like Arnolds arm! ;)

              I used to scroll through the entire thing every morning to find my 150’ich lines that was out of sync with the other lines. In this example I have deleted most of the data, but kept the sort of lines that have been creating trouble for me.
              42010b31-b604-4889-8b3f-38bb0edbbed9-image.png
              (Edited some names and numbers as it might not be 100% ok to share this sort of data unedited…)

              The top two lines are as the rest should be to be able to propperly crunch these lines in Excel.

              So when the document is exported from SAP it contains both single CR and LF in the middle of the lines.
              This is consistant on every export I do, so it has to do something with the way that given line is entered into SAP. I do not have the ability to change them in SAP, so I have to work with what I have.

              The solution untill now has been to search for ^(.*?|^\r)\n
              That would give me most of the lines that are affected. I would then simply delete the returned LF. That would change it into a single line starting with a TAB.

              I would still have to scroll for the single CR’s, but there is normally about 10 of them. I tried searching using (?-s)^.*?\r(?!\n) as suggested by Alan, but it did not give any results.

              I also tried changing the EOL conversion as suggested by Terry, but that changed the line endings, but not the layout. Looking back at my initial post, I should have been clearer on what I wanted to acomplish in the end…

              Thinking about it… Actually searching for every line that does not end with CR LF would also do the trick…

              But the best (would be fixing SAP, but not an option here) would be to search and replace the single CR’s with a space, and then do the same for single LF. I tried doing that searching for ^(.*?|^\r)\n and replacing with a space. That was a no-no. It just exploded on me creating even more mess :(

              Alan KilbornA 1 Reply Last reply Reply Quote 0
              • guy038G
                guy038
                last edited by guy038

                Hello, @bjørn-didriksen, @alan-kilborn, @terry-r and All,

                Bjorn, I suppose that the following regex S/R should do the trick !

                SEARCH \r(?!\n)|(?<!\r)\n

                REPLACE \x20


                Notes :

                • Tick the Wrap around option

                • Select the Regular expression search mode

                • Click on the Replace All button

                • This reges S/R searches for :

                  • Any CR char, not followed with a LF char
                    OR
                  • Any LF char, not preceded with a CR char

                • And replaces, both of them, with a space character


                If you want to easily find the location of all the inserted space characters, try, FIRST, to replace any single EOL character with the ■ Unicode character ( U+25A0 ), using this second regex S/R :

                SEARCH \r(?!\n)|(?<!\r)\n

                REPLACE \x{25A0}

                Best Regards,

                guy038

                Bjørn DidriksenB 1 Reply Last reply Reply Quote 3
                • Bjørn DidriksenB
                  Bjørn Didriksen @guy038
                  last edited by

                  @guy038 said in How to find all lines ending with only CR:

                  \r(?!\n)|(?<!\r)\n

                  Man! I want to kiss you!
                  I am so happy now that you have abolutely no idea!

                  It worked! I had to do a double check against a copy of the file, and it seems to have picked up every single instance of these :)

                  1 Reply Last reply Reply Quote 1
                  • Alan KilbornA
                    Alan Kilborn @Bjørn Didriksen
                    last edited by

                    @Bjørn-Didriksen said in How to find all lines ending with only CR:

                    None of these seems to help me fix the problem totally, so I might need to give the entire story on this one…

                    Yea, well, if you’d have done that in the first place, we could have immediately jumped to the solution you needed.

                    Bjørn DidriksenB 1 Reply Last reply Reply Quote 0
                    • Bjørn DidriksenB
                      Bjørn Didriksen @Alan Kilborn
                      last edited by

                      @Alan-Kilborn Just trying to be sort of vague to cover my ass here as I am sharing data from a customers SAP system…
                      But NEXT time! ;)

                      Everything went into the user manual I am writing for the use of that excel sheet, with references back here.

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