Trim length between two characters



  • What I need to do is trim the last character between the , and , if the length is 12 or greater which would then make the length 11.

    This is before

    #,610079544260,1
    #,205300099864,1
    #,65433455333,5
    #,205300022008,1
    #,3360,20
    #,718103267854,2
    #,67764334775,8

    This is what it should look like after

    #,61007954426,1
    #,20530009986,1
    #,65433455333,5
    #,20530002200,1
    #,3360,20
    #,71810326785,2
    #,67764334775,8

    Thank you



  • @Sean

    You might try:

    Find: ^(#,\d{11})\d(?=,)
    Replace: \1
    Search mode: Regular expression



  • @Alan-Kilborn

    Perfect thank you very much.



  • If the file is not too long, you can always use Column Mode with the Alt key. I do similar operations on CSV files all the time. Regular expressions are more elegant though.



  • @Steven-Haymes

    I made the assumption that the OP may have other lines (that are longer) intermingled with the ones shown that he doesn’t want affected. Otherwise, yes, you are right, this is a good application of column mode editing, perhaps in combination with the Begin/End Select command, which also works with column mode editing, although it is a bit obscure (you have to get into column mode as you are positioning the caret for the “end select”).



  • Hello, @sean, @alan-kilborn, @steven-haymes, and All,

    I’m afraid that, in order to use the column mode, in a reliable way, we should align the comma symbols, padding preceding positions with spaces chars !

    So, this time, the regex solution seems to be more secure ;-))

    For instance, with that sample text, below :

    #,6100795442608787,1
    #,205300099864,1
    #,65433455333,5
    #,20530002200815618844,1
    #,3360,20
    #,718103267854,2
    #,67764334775,8
    

    Here is a regex S/R which deletes any non-null range of digits, after the 11th, till the second comma of each line :

    • SEARCH ^#,\d{11}\K\d+(?=,)

    • REPLACE Leave EMPTY

    • Tick the Wrap around

    • Select the Regular expression search mode

    • Click, exclusively, on the Replace All button

    And, we would obtain the expected result :

    #,61007954426,1
    #,20530009986,1
    #,65433455333,5
    #,20530002200,1
    #,3360,20
    #,71810326785,2
    #,67764334775,8
    

    Best Regards,

    guy038


Log in to reply