Community
    • Login

    Replace space with commas but for first 28 characters per line

    Scheduled Pinned Locked Moved General Discussion
    formatting text
    4 Posts 4 Posters 564 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.
    • David CummingsD
      David Cummings
      last edited by

      Each day I get a text file with 2,000+ lines like this -

      406D69 NPT04S G-JMCH B734 West Atlantic UK -------- 25025 24450 1250 1250 30/01/2023 23:54:46 30/01/2023 23:59:31

      I wish to have the output changed to -
      406D69,NPT04S,G-JMCH,B734,West Atlantic UK -------- 25025 24450 1250 1250 30/01/2023 23:54:46 30/01/2023 23:59:31

      Ideally I want the output to be -

      406D69,NPT04S,G-JMCH,B734,West Atlantic UK

      I would have suggested that anything including “--------” and after is removed but sometimes I get lines like this -

      45CE4E -------- OY-SRN B762 Maersk Air Cargo -------- 17600 22950 0000 0000 30/01/2023 03:35:36 30/01/2023 19:58:48

      but getting the commas only into the first 28 columns would be a awesome start, can notepad++ do this?

      Alan KilbornA dinkumoilD 2 Replies Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @David Cummings
        last edited by

        This post is deleted!
        1 Reply Last reply Reply Quote 0
        • dinkumoilD
          dinkumoil @David Cummings
          last edited by dinkumoil

          @David-Cummings

          At first I want to hint you that this forum provides the ability to post data in a way that the forum software doesn’t change/reformat your input data:

          1. Surround your data with grave characters or
          2. click the code button in the toolbar above the text input area where you type your comments

          83aaa26a-d9ac-45a4-82cc-97b957c05fc6-grafik.png

          and replace the pre-selected text showing up with your data. Using method 1. your posting would be then:

          @David-Cummings said in Replace space with commas but for first 28 characters per line:

          Each day I get a text file with 2,000+ lines like this -
          406D69 NPT04S G-JMCH B734 West Atlantic UK -------- 25025 24450 1250 1250 30/01/2023 23:54:46 30/01/2023 23:59:31
          I wish to have the output changed to -
          406D69,NPT04S,G-JMCH,B734,West Atlantic UK -------- 25025 24450 1250 1250 30/01/2023 23:54:46 30/01/2023 23:59:31
          Ideally I want the output to be -
          406D69,NPT04S,G-JMCH,B734,West Atlantic UK
          I would have suggested that anything including -------- and after is removed but sometimes I get lines like this -
          45CE4E -------- OY-SRN B762 Maersk Air Cargo -------- 17600 22950 0000 0000 30/01/2023 03:35:36 30/01/2023 19:58:48
          but getting the commas only into the first 28 columns would be a awesome start, can notepad++ do this?


          To answer your question: Yes, Notepad++ can help you with your task. Provided that the line

          45CE4E -------- OY-SRN B762 Maersk Air Cargo -------- 17600 22950 0000 0000 30/01/2023 03:35:36 30/01/2023 19:58:48

          should be converted to

          45CE4E,--------,OY-SRN,B762,Maersk Air Cargo

          try the following:

          Open Search & Replace dialog and set the following options:

          • Find what: ^([^\h]+)\h+([^\h]+)\h+([^\h]+)\h+([^\h]+)\h+([\w ]+(?= -))(.+)$
          • Replace with: $1,$2,$3,$4,$5
          • Match whole word only: Unchecked
          • Wrap around: Checked
          • Search mode: Set option Regular expression and uncheck . matches newline

          Finally click Replace all button.

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

            Hello, @david-cummings, @alan-kilborn and All,

            Not difficult with regexes !


            So, let’s start with this INPUT text :

            406D69 NPT04S G-JMCH B734 West Atlantic UK -------- 25025 24450 1250 1250 30/01/2023 23:54:46 30/01/2023 23:59:31
            
            • Open the Replace dialog ( Ctrl + H )

            • SEARCH (?x-s) ^ (\S+) \x20 (\S+) \x20 (\S+) \x20 (\S+) \x20 (\S+) | \x20+ -------- .+

            • REPLACE (?1\1,\2,\3,\4,\5)

            • Untick all box options

            • Tick the Wrap around option

            • Select the Regular expression serach mode

            • Click on the Replace All button

            => You should get your expected OUTPUT text :

            406D69,NPT04S,G-JMCH,B734,West Atlantic UK
            

            If OK, I’ll explain you how it works, next time !

            Note that I use the Free Spacing mode (?x), in the search regex, in order to separate the main parts for a better comprehension

            Best Regards,

            guy038

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