Loading ...
Sorry, an error occurred while loading the content.

23902Need help with RegExp to delete columns

Expand Messages
  • joy8388608
    Jul 9, 2013
      Another 'simple' clip gets harder than I thought... I can usually follow RegExp fairly well but can not always come up with what I need myself. Especially not as well as some regulars here.

      I wanted to create a clip that would allow me to delete a specified column.
      For now, assume comma delimited and one column at a time.
      Data can be anything and any length including zero.
      We can also assume no trailing delimiters (commas).

      Thanks!
      Joy

      If a general replace works then fine, but I was trying to keep it fairly simple with code like:
      ; Do not take my replace statements seriously!!!
      ^!Set %ColToRem1%=^?[Col=2]
      ^!If ^%ColToRem1% > 2 GT2
      ^!If ^%ColToRem1% = 2 EQ2

      :EQ1
      ^!REPLACE "^.*?,?(.*(\R|\Z))" >> "$1" WRSA
      ^!Goto END

      :EQ2
      ^!REPLACE "^(?:.*?)\K(,+)(.*?)(\R|\Z)" >> "$2$3" WRSA
      ^!Goto END

      :GT2
      ^!Set %ColToRem_minus_1%=^$Calc(^%ColToRem1% - 1)$
      ^!REPLACE "^(?:(?:.*?,){^%ColToRem_minus_1%})\K.*?\t(.*(\R|\Z))" >> "$1" WRSA
      ^!Goto END


      Test Data
      A11
      11,22,33,44,BB
      ,22,33,CC
      ,22,33,44,DD
      11,,33,44,EE
      11,,,44,FF
      11,22,,44,GG
      11,,,,HH


      If I wanted column 1 to be deleted, I would be left with
      <empty line>
      22,33,44,BB
      22,33,CC
      22,33,44,DD
      ,33,44,EE
      ,,44,FF
      22,,44,GG
      ,,,HH


      If I wanted column 2 to be deleted, I would be left with
      A11
      11,33,44,BB
      ,33,CC
      ,33,44,DD
      11,33,44,EE
      11,,44,FF
      11,,44,GG
      11,,,HH


      If I wanted column 3 to be deleted, I would be left with
      A11
      11,22,44,BB
      ,22,CC
      ,22,44,DD
      11,,44,EE
      11,,44,FF
      11,22,44,GG
      11,,,HH
    • Show all 6 messages in this topic