• Login
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 570 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.
  • D
    David Cummings
    last edited by Jan 31, 2023, 10:44 AM

    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?

    A D 2 Replies Last reply Jan 31, 2023, 12:11 PM Reply Quote 0
    • A
      Alan Kilborn @David Cummings
      last edited by Jan 31, 2023, 12:11 PM

      This post is deleted!
      1 Reply Last reply Reply Quote 0
      • D
        dinkumoil @David Cummings
        last edited by dinkumoil Jan 31, 2023, 1:38 PM Jan 31, 2023, 1:25 PM

        @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
        • G
          guy038
          last edited by guy038 Jan 31, 2023, 1:35 PM Jan 31, 2023, 1:27 PM

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