sort -t"," -n -k5 file.csv # according to the 5th field NUMERICALLY!!

sort a csv file according to a particular n th field numerically (quicker than excel)

-n is very important, otherwise it's string comparison
Sample Output
pielberg,Steven,18,dec,1946
Gellar,Sarah_Michelle,14,apr,1977
Bryant,Kobe1,23,aug,1978
Bryant,Kobe4,23,sep,1979
Harnois,Elisabeth,26,may,1979
Underwood,Carrie,10,mar,1983
Ryan,Michelle,22,apr,1984
Belle,Camilla,2,oct,1986
Sharapova,Maria,19,apr,1987
Bryant,Kobe3,23,sep,1998
Bryant,Kobe2,24,aug,2000


the file.csv is:
pielberg,Steven,18,dec,1946
Bryant,Kobe2,24,aug,2000
Gellar,Sarah_Michelle,14,apr,1977
Bryant,Kobe4,23,sep,1979
Harnois,Elisabeth,26,may,1979
Underwood,Carrie,10,mar,1983
Bryant,Kobe1,23,aug,1978
Ryan,Michelle,22,apr,1984
Belle,Camilla,2,oct,1986
Sharapova,Maria,19,apr,1987
Bryant,Kobe3,23,sep,1998

0
By: dexterhu
2011-03-11 09:09:57

These Might Interest You

  • ls -al gives all files, sort +4n sorts by 5th field numerically


    0
    ls -al | sort +4n
    ankush108 · 2012-06-26 19:20:05 0
  • Sometimes you want to work on data sheets by using heirloom unic commands like cut, paste, sed, sort, wc and good old awk. But your user works on Microsoft Excel spreadsheet. The idea: 1) ask your user to save it as "Unicode Text" from Microsoft Excel and send the document to you; 2) use the given command to convert it to UTF-8 text. We carefully convert "\r\n" to local end-of-line character; and to convert "\n" (in Excel, means linebreak within the table cell") to "\r", which is carrier return but not end-of-line in Unix. If the "\n" is not replaced with "\r", for example, wc -l will report incorrect column number.


    0
    iconv -f UTF16LE -t UTF-8 < SOURCE | awk 'BEGIN { RS="\r\n";} { gsub("\n", "\r"); print;}' > TARGET
    zhangweiwu · 2010-04-04 07:16:57 0
  • Tells sort to ignore all characters before the Xth position in the first field per line. If you have a list of items one per line and want to ignore the first two characters for sorting purposes, you would type "sort -k1.3". Change the "1" to change the field being sorted. The decimal value is the offset in the specified field to sort by.


    3
    sort -k1.x
    leper421 · 2009-06-16 00:04:21 0
  • The output format is given by the -printf parameter: %T@ = modify time in seconds since Jan. 1, 1970, 00:00 GMT, with fractional part. Mandatory, hidden in the end. %TY-%Tm-%Td %TH:%TM:%.2TS = modify time as YYYY-MM-DD HH:MM:SS. Optional. %p = file path Refer to http://linux.die.net/man/1/find for more about -printf formatting. ------------------------ sort -nr = sort numerically and reverse (higher values - most recent timestamp - first) head -n 5 = get only 5 first lines (change 5 to whatever you want) cut -f2- -d" " = trim first field (timestamp, used only for sorting) ------------------------ Very useful for building scripts for detecting malicious files upload and malware injections. Show Sample Output


    6
    find . -type f -printf '%T@ %TY-%Tm-%Td %TH:%TM:%.2TS %p\n' | sort -nr | head -n 5 | cut -f2- -d" "
    paulera · 2016-03-23 11:56:39 0
  • Provides numerically sorted human readable du output. I so wish there was just a du flag for this. Show Sample Output


    4
    du -x --max-depth=1 | sort -n | awk '{ print $2 }' | xargs du -hx --max-depth=0
    d34dh0r53 · 2010-02-18 19:46:47 2
  • Notes: 1) -n-1 means sort key is the last field 2) -l is important if each separate record is on a new line (usually so for text files) 3) -j tells msort not to create log file (msort.log) in the working directory 4) may need to install msort package. 5) msort does lot more. Check man msort Show Sample Output


    4
    file /bin/* | msort -j -l -n-1 -n2 2> /dev/null
    b_t · 2010-10-05 00:37:33 1

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: