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

Re: complex sort

Expand Messages
  • Juan M. Medina
    I would to this using some sort of database, which would be easier. However, ... First line appends a # sign to each line that is not an END tag. Second Line
    Message 1 of 7 , Jun 23, 2003
    • 0 Attachment
      I would to this using some sort of database, which would be easier. However,
      since this is list is to do stuff with VIM, here goes a solution:

      :v/END:VCARD/normal A#
      :v/END:VCARD/normal J
      100@:
      :%s:\(.*# N\:\(.*\);#.*\):\2#\1
      :%!sort
      :%s:.\{-}#\(.*\):\1

      First line appends a "#" sign to each line that is not an END tag.
      Second Line joins the first set of lines that do not match and END tag.
      Third line repeats the Second line a hundred times (see :help @:). You may
      want to increase this number or repeat the command until you are sure all
      your records are in one line.
      Fourth line appends the N: tag contents to the beginning of the line.
      Fifth Line applies the sort.
      Sixth line removes the name from the beginning of each line.
      Seventh line restores the records to be multiple lines.

      Note this assumes your name tag values end in a semicolon as in your
      example. Also if your data contains a # sign you need to change that on line
      1 to a sign that is not used in your data.

      This is the data I used to test the logic:

      BEGIN:VCARD
      SOME1:FDSFSD
      SOME2:DSFSDFJ
      N:JOHNSON,SCOTT;
      MORE:SLFKJSDKLFJ
      END:VCARD
      BEGIN:VCARD
      N:COPLAND,AARON;
      BLUE:FDSLFKSJ
      END:VCARD
      BEGIN:VCARD
      LINGO:DFASDKFSD
      ITEM:SDFLJKFDS
      N:ZORG,ELEANOR;
      END:VCARD
      BEGIN:VCARD
      OTHER:DSFSDFSD
      N:MILES,ADAM;
      ANOTHER:DSFSDF
      COUNTER:DSFSDJFSDL
      END:VCARD

      This was the result after applying all the commands:

      BEGIN:VCARD
      N:COPLAND,AARON;
      BLUE:FDSLFKSJ
      END:VCARD
      BEGIN:VCARD
      SOME1:FDSFSD
      SOME2:DSFSDFJ
      N:JOHNSON,SCOTT;
      MORE:SLFKJSDKLFJ
      END:VCARD
      BEGIN:VCARD
      OTHER:DSFSDFSD
      N:MILES,ADAM;
      ANOTHER:DSFSDF
      COUNTER:DSFSDJFSDL
      END:VCARD
      BEGIN:VCARD
      LINGO:DFASDKFSD
      ITEM:SDFLJKFDS
      N:ZORG,ELEANOR;
      END:VCARD

      ----- Original Message -----
      From: "A. S. Budden" <vim.mail@...>
      To: <vim@...>
      Sent: Monday, June 23, 2003 9:37 AM
      Subject: complex sort


      > Hi there,
      >
      > Given a file of a format similar to:
      >
      > -- snip --
      > BEGIN:VCARD
      > ..
      > N:LASTNAME,FIRSTNAME;
      > ..
      > END:VCARD
      >
      > BEGIN:VCARD
      > -- snip --
      >
      > Is there any way to sort these vcards alphabetically according to
      > lastname then firstname (i.e. according to the ^N: line)?
      >
      > Al
      >
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.