2

I've got a csv file which I need to search in. The problem is that there's too much data in it and looking through the output is kind of problematic. It looks like this

col1,col2,...col23
1,123,...,12.01
43,1123,...,142.455
12,63,...,723.1122
...

So, after applying some grep pattern to it I'd like to have the output sorted by the col23 in descending order. col23 contains floating point numbers. Is there a way to do that?

I mean something like this:

grep "pattern" the_file.csv | ...sort it
3

Use:

grep "pattern" the_file.csv | sort -t, -nk 23 | column -ts,

or

grep "pattern" the_file.csv | sed 's/,/ /g' | sort -nk 23 | column -t

with exponential notation use sort -g instead of sort -n

Explanation:

  • sort -k 23: sort on column number 23
  • sort -n: compare according to string numerical value
  • sort -g: compare according to general numerical value
  • sort -t,: sort with delimiter ,
  • column -t: make columns based on whitespaces or using delimiter (-s<delimiter>)
  • sed 's/,/ /g': substitute (s) any occurences of , with a space ' ' on all lines (g)
  • That's some magic. It works. Thanks much. Can't you give a little explanation? – St.Antario Dec 23 '16 at 7:40
  • Thank you. BTW, what if some numbers in the column have exponential format? Like 1.231231235E8? – St.Antario Dec 23 '16 at 7:49
  • 1
    @St.Antario you're welcome. See my updated answer. – rda Dec 23 '16 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.