Sort and list values from CSV

I have a CSV with below values

name,city,2,country
name,city,15,country
abc,wq,10,afdfd,
qeqe,ewqre,1,wqew

I need to sort them in ascending order based on the value of column 3 and then , pick the rows with values less than 10 and be able to display the columns in that row. Can anyone please help me out.

Try

$ cat file
name,city,2,country
name,city,15,country
abc,wq,10,afdfd,
qeqe,ewqre,1,wqew
$ awk -F, '$3<10' <(sort -t"," -nk3 file)

Resulting

qeqe,ewqre,1,wqew
name,city,2,country

Using GNU awk asort function:

gawk -F, '
        {
                A[++c] = $3
                R[$3] = $0
        }
        END {
                n = asort ( A )
                for ( i = 1; i <= n; i++ )
                {
                        if ( A < 10 )
                                print R[A]
                }
        }
' file.csv

Based on Akshay Hegde's proposal, this should be somewhat faster:

awk -F, '$3 > 9 {exit}1' <(sort -t, -nk3,3  file)
qeqe,ewqre,1,wqew
name,city,2,country
1 Like

Hi ,

Thanks for the reply. The command works when i run it separately. but when i put it in a function call like below it gives error:

function SortOutput
{
ResultCSV=$1
Expirelt60=$OUTPUT_PATH/CertDetails.expirylt60.csv
 
awk -F, '$6<60' <(sort -t"," -nk3 $ResultCSV)  >>$Expirelt60


}

Try like this

#!/bin/bash

function Sort_out(){
                            awk -F, '$3 > 9 {exit}1' <(sort -t, -nk3,3  $1) >$2
                   }

# Calling Function
Sort_out  Input_File Output_File

If you want to run from terminal try like this

Here is your input file

$ cat Input_File
name,city,2,country
name,city,15,country
abc,wq,10,afdfd,
qeqe,ewqre,1,wqew

This will be your script

$ cat Sort_Script.sh
#!/bin/bash

function Sort_out(){
                        awk -F, '$3 > 9 {exit}1' <(sort -t, -nk3,3  $1) >$2
                   }

# Calling Function

Sort_out  $1 $2

On Terminal you should run like this

$ bash Sort_Script.sh Input_File  Output_File

Output is written to filename Output_File

$ cat Output_File
qeqe,ewqre,1,wqew
name,city,2,country

Hi Akshay ,

I'm getting the below error

syntax error near unexpected token `('
 ` awk -F, '$6 < 60 {exit}1' <(sort -t, -nk3,3 $1)  >>$2'

How did you try ? which is you bash ?

Hi Askshay ,

Below is my code:

#!/bin/sh
#
SortOutput $OutputCSV $abc

function SortOutput
{
 awk -F, '$6 < 60 {exit}1' <(sort -t, -nk3,3 $1)  >>$2
}


function should be called after defining it

#!/bin/sh


function SortOutput()
{
 awk -F, '$6 < 60 {exit}1' <(sort -t, -nk3,3 $1)  >>$2
}
SortOutput $OutputCSV $abc

[/CODE] () was missing

a) your sample did not have 6 fields, so don't expect anything to be printed with above proposal.
b) SHOULD your input have 6 fields or more, and $6 being the relevant one, you should also make it the sort key: -k6,6

Thanks for your help on this. Now i have a new requirement as mentioned below:

I have a CSV with below values

name,city,2,country
name,city,15,country
abc,wq,10,afdfd,
qeqe,ewqre,1,wqew
abd, we,2,qwqe

I need to sort them in ascending order based on the value of column 3 and then , pick the rows with values less than 10 and be able to display the columns in that row. Also if the column 3 values are same , I need to print only one row:

for eg:
Only one of the row needs to printed.

name,city,2,country 
abd, we,2,qwqe