#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Bash script to reorder csv


    hi guys,

    im fairly new to unix and bash scripts and therefore your help would really be appreciated.

    i need to write a bash script that will take a csv file, and reorder the data and output to another csv file.

    The source csv file will look something like this:
    HEAD,671061,Add,SS 2011,1,??? NEW Hanger,IPL/CML/BDCM/BCA
    LOT,671061,1,30/12/2010,31/01/2011,,
    LINE,671061,43825012,2,30/12/2010,,
    LINE,671061,43825174,1,30/12/2010,,
    LINE,671061,43825256,1,30/12/2010,,
    LINE,671061,43825262,1,30/12/2010,,
    LOT,671061,2,20/01/2011,22/02/2011,,
    LINE,671061,43825012,2,20/01/2011,,
    LINE,671061,43825029,2,20/01/2011,,
    LINE,671061,43825256,1,20/01/2011,,
    LINE,671061,43825262,1,20/01/2011,,
    LOT,671061,3,03/03/2011,04/04/2011,,
    LINE,671061,43825012,2,03/03/2011,,
    LINE,671061,43825262,1,28/04/2011,,
    HEAD,671532,Add,4,MS-CWE-5 CUFF,,
    LOT,671532,1,07/04/2011,09/05/2011,,
    LINE,671532,76595017,1,07/04/2011
    LINE,671532,76595052,2,07/04/2011
    LINE,671532,76595069,1,07/04/2011

    i need to then sort it by field 2, and then group by header type, and then sort all line items by date, to produce the following:

    HEAD,671061,Add,SS 2011,1,??? NEW Hanger,IPL/CML/BDCM/BCA
    LINE,671061,43825012,2,30/12/2010,,
    LINE,671061,43825174,1,30/12/2010,,
    LINE,671061,43825256,1,30/12/2010,,
    LINE,671061,43825262,1,30/12/2010,,
    LINE,671061,43825012,2,20/01/2011,,
    LINE,671061,43825029,2,20/01/2011,,
    LINE,671061,43825256,1,20/01/2011,,
    LINE,671061,43825262,1,20/01/2011,,
    LINE,671061,43825012,2,03/03/2011,,
    LINE,671061,43825262,1,28/04/2011,,
    LOT,671061,1,30/12/2010,31/01/2011,,
    LOT,671061,2,20/01/2011,22/02/2011,,
    LOT,671061,3,03/03/2011,04/04/2011,,
    HEAD,671532,Add,4,MS-CWE-5 CUFF,,
    LINE,671532,76595017,1,07/04/2011,,
    LINE,671532,76595052,2,07/04/2011
    LINE,671532,76595069,1,07/04/2011
    LOT,671532,1,07/04/2011,09/05/2011
    HEAD,671533,Add,4,MS-CWE-5 CUFF
    LINE,671533,76595017,1,07/04/2011
    LINE,671533,76595023,4,07/04/2011
    LINE,671533,76595069,1,07/04/2011
    LINE,671533,76595017,1,21/04/2011
    LINE,671533,76595023,4,21/04/2011
    LOT,671533,1,07/04/2011,09/05/2011
    LOT,671533,2,21/04/2011,23/05/2011

    Any help would be much appreciated

    thanks
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    844
    Rep Power
    387

    Wink Sort or not to sort...


    Originally Posted by daz_20
    ... etc ...
    i need to then sort it by field 2, and then group by header type, and then sort all line items by date, to produce the following:
    ... etc ...
    Any help would be much appreciated

    thanks
    Try this:
    Code:
    sort -t, -k 2,2 -k 1,1 -k 5.7 -k 5.3 -k 5.1 FILE1.txt

IMN logo majestic logo threadwatch logo seochat tools logo