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

59576Re: finding null fields in bar-separated records

Expand Messages
  • Antony Scriven
    Jun 1, 2005
    • 0 Attachment
      Hello

      On May 31, Hari Krishna Dara wrote:

      > On Tue, 31 May 2005 at 6:46am, jkilbour@... wrote:
      >
      > > I would like to identify the null fields in a set of
      > > files (which have different numbers of fields; i.e. to
      > > find not just the number of fields that are null but
      > > also which fields are null. Is this possible using vim
      > > regular expressions?
      >
      > Depending on what exactly you want to do with them, you
      > might be able to create multiple solutions. May be you
      > can first number all of the fields and then search for
      > those that are empty to lookup them up.
      >
      > function! Submatch()
      > let g:idx = g:idx + 1
      > let match = submatch(1)
      > return (match == '' ? g:idx.':'.'<null>' : match)
      > endfunction
      >
      > let g:idx = 0 | s/|\([^|]*|\@=\)/\='|'.Submatch()/g
      >
      > The above will transform
      >
      > |one|two|three||five||seven|
      >
      > into
      >
      > |one|two|three|4:<null>|five|6:<null>|seven|
      >
      > [...]

      This is somewhat shorter and transforms the whole buffer (if
      I understand the problem correctly; I missed the original
      mail):

      %s/\(^.*|\)\@<=|/\=strlen(substitute(submatch(1),'[^|]','','g')).':<null>|'/g

      But I offer this mostly for interesting ways to use \@<=. It
      is, IMO, unmaintainable. I think \= plus a function, as Hari
      has done, is normally the best approach for sort of thing.

      Antony
    • Show all 11 messages in this topic