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

59567Re: finding null fields in bar-separated records

Expand Messages
  • Hari Krishna Dara
    May 31, 2005
    • 0 Attachment
      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|

      All that you need to do then is to search for nulls using a pattern such
      as "\d\+:<null>". The actual regex to use to do the above substitution
      will depend on exact specifications, such as can you have a "|"
      character inside a field, and if so how you escape them. I am not a
      regex guru myself, but if you need further help, you can describe your
      needs in more details, for me or others on the list to come up with the
      right pattern.

      There are many regex gurus on this list, so I won't be surprised to see
      a much simpler/easy to use solution. However, if you want to deal with a
      programmatic approach, you can take a look at my multvals.vim plugin to
      iterate over the fields and do something with them.

      call MvIterCreate('|one|two|three||five||seven|', '|', 'Iter')
      let n = 0
      while MvIterHasNext('Iter')
      let ele = MvIterNext('Iter')
      if ele == ''
      echo 'Found null at: ' . n
      endif
      let n = n + 1
      endwhile
      call MvIterDestroy('Iter')

      PS: Multvals treats the first "|" in the string also as a separator
      resulting in one extra field, but you should be able to workaround that.

      --
      HTH,
      Hari



      __________________________________
      Do you Yahoo!?
      Yahoo! Small Business - Try our new Resources site
      http://smallbusiness.yahoo.com/resources/
    • Show all 11 messages in this topic