Click here to Skip to main content
11,790,816 members (58,908 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: Cygwin CSV
Hi there,

I'm using Cygwin to sort a csv file, but I can't get it to ignore the header any ideas?
sort -t',' -k5,2 c:\tmp\CI-tosplit.csv > c:\tmp\CI-DEN-sorted.csv

-t"," - seperator
-k5 column ,2 I thought meant start at line 2 but I guess i'm wrong.
Posted 4-Jun-13 2:21am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

tail -n 2 c:\tmp\CI-tosplit.csv | sort -t',' -k5 c:\tmp\CI-tosplit.csv > c:\tmp\CI-DEN-sorted.csv
to start from line 2

{Edit - took out the ,2 ... too cut&paste keen!]
Paul Hayman at 4-Jun-13 8:34am
Sorry this is using windows:

'tail' is not recognized as an internal or external command,
operable program or batch file.
CHill60 at 4-Jun-13 9:15am
sorry ... must have some additional stuff installed. I'll go ask and in the meantime will delete my (dodgy) solution so your post goes back into the Unanswered pot
Paul Hayman at 4-Jun-13 9:10am
Ah ok I got it, but the header is now the footer lol....
CHill60 at 4-Jun-13 9:17am
try ...(head -n 1 c:\tmp\CI-tosplit.csv && tail -n +1 c:\tmp\CI-tosplit.csv | sort ... etc
Paul Hayman at 4-Jun-13 9:31am
OK Thanks so much, I think it's getting close but still not quite right:

C:\cygwin\bin\head -n 1 /cygdrive/c/tmp/CI-tosplit.csv && C:\cygwin\bin\tail -n +1 /cygdrive/c/tmp/CI-tosplit.csv | C:\cygwin\bin\sort -t',' -k5 /cygdrive/c/tmp/CI-tosplit.csv > c:\tmp\CI-DEN-sorted.csv

Gives error:
c:\tmp>C:\cygwin\bin\head -n 1 /cygdrive/c/tmp/CI-tosplit.csv && C:\cygwin\bin
\tail -n +1 /cygdrive/c/tmp/CI-tosplit.csv | C:\cygwin\bin\sort -t',' -k5 /cy
gdrive/c/tmp/CI-tosplit.csv 1>c:\tmp\CI-DEN-sorted.csv
/usr/bin/tail: write error

It's got the headers but not sure about the write error?? any Ideas?
CHill60 at 4-Jun-13 9:50am
Not sure (and nowhere near a machine I can check this out myself unfortunately) but it could be the target file name being in "windows format" rather than unix-style. Can't remember how to get around that I'm afraid. If you drop the redirection to a file does the error "go away"?
Paul Hayman at 5-Jun-13 8:00am
No Still get the error. arghhh I thought it woul dbe easy to sort a csv from command line.... I know it's windows but it's a work pc...
CHill60 at 5-Jun-13 8:36am
Comment crashed! If you can use a Windows cmd file instead of cygwin then you could try something like more c:\tmp\CI-tosplit.csv +2 | sort > c:\tmp\CI-DEN-sorted.csv
Paul Hayman at 5-Jun-13 8:57am
Still no luck i'm afraid....

Creates a file with the following: 2 cells c:\tmp>C:\cygwin\bin\sort -t" " -k6,2 c:\tmp\test.csv 1>c:\tmp\test-sorted.csv"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 385
1 ppolymorphe 339
2 Richard MacCutchan 118
3 Maciej Los 70
4 George Swan 70
0 OriginalGriff 1,154
1 Maciej Los 825
2 KrunalRohit 686
3 ppolymorphe 660
4 CPallini 621

Advertise | Privacy | Mobile
Web03 | 2.8.1509028.1 | Last Updated 4 Jun 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100