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

Re: how to sort a file

Expand Messages
  • Vlad Ghitulescu
    ... Regards, Vlad ... -- NEU +++ DSL Komplett von GMX +++ http://www.gmx.net/de/go/dsl GMX DSL-Netzanschluss + Tarif zum superg├╝nstigen Komplett-Preis!
    Message 1 of 5 , Nov 30, 2004
    • 0 Attachment
      :%!sort /+16 works fine for me, thanks!

      Regards,
      Vlad


      > > I have a big(about 300.000 lines) CSV-file that looks something like
      > this:
      > >
      > > "ABINB2EK7SF1","78","998079","100723047","008","0",...
      > > "ABINB2Y1S92N","79","998884","100723088","001","0",...
      > [cut]
      > > I want to sort this file (with gVim under Windows XP) with regard to the
      > > fields from column 16 to 50.
      >
      > Well, from within Win2k/XP, the Dos "sort" command should do the
      > trick...they added a "/+n" option to start sorting from a
      > particular character of each line. If sorting by the Nth
      > character is what you want to do, you should be able to just do
      >
      > C:\temp\> sort /+16 < unsorted.csv > sorted.csv
      >
      > and get the resulting file. However, this is the vim list, so
      > I'll throw in some vim solutions.
      >
      > 1:
      >
      > :%!sort /+16
      >
      > 2:
      > Dr. Chip has the following tip:
      > http://www.vim.org/tips/tip.php?tip_id=588
      >
      > 3:
      > visually (block-wise with ^V) select the desired columns, copy
      > them to the beginning of the line, and use a generic "sort" such
      > as ":%!sort", then clean up the copied characters.
      >
      > 4:
      > if they're not nicely column-aligned, but rather CSV, you can
      > use a :s expression to move the Nth column before the first
      > column. The following exchanges the first two columns (\{2})
      > with the following column, sorts them, them moves the first
      > column back before to its original place.
      > :%s/\(\_("[^"]*"\s*,\s*\)\{2}\)\("[^"]*"\s*,\s*\)/\2\1
      > :%!sort
      > :%s/\("[^"]*"\s*,\s*\)\(\_("[^"]*"\s*,\s*\)\{2}\)/\2\1
      >
      >
      > Plenty of Vim fun to keep you busy :)
      >
      > -tim
      >
      >
      >
      >
      >
      >
      >
      >

      --
      NEU +++ DSL Komplett von GMX +++ http://www.gmx.net/de/go/dsl
      GMX DSL-Netzanschluss + Tarif zum superg´┐Żnstigen Komplett-Preis!
    Your message has been successfully submitted and would be delivered to recipients shortly.