Sort and list values from CSV


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort and list values from CSV
# 1  
Old 11-04-2013
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.
# 2  
Old 11-04-2013
Try

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

Code:
$ awk -F, '$3<10' <(sort -t"," -nk3 file)

Resulting
Code:
qeqe,ewqre,1,wqew
name,city,2,country

# 3  
Old 11-04-2013
Using GNU awk asort function:
Code:
gawk -F, '
        {
                A[++c] = $3
                R[$3] = $0
        }
        END {
                n = asort ( A )
                for ( i = 1; i <= n; i++ )
                {
                        if ( A[i] < 10 )
                                print R[A[i]]
                }
        }
' file.csv

# 4  
Old 11-04-2013
Based on Akshay Hegde's proposal, this should be somewhat faster:
Code:
awk -F, '$3 > 9 {exit}1' <(sort -t, -nk3,3  file)
qeqe,ewqre,1,wqew
name,city,2,country

This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-05-2013
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:
Code:
function SortOutput
{
ResultCSV=$1
Expirelt60=$OUTPUT_PATH/CertDetails.expirylt60.csv
 
awk -F, '$6<60' <(sort -t"," -nk3 $ResultCSV)  >>$Expirelt60


}

# 6  
Old 11-05-2013
Try like this

Code:
#!/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
Code:
$ cat Input_File
name,city,2,country
name,city,15,country
abc,wq,10,afdfd,
qeqe,ewqre,1,wqew

This will be your script
Code:
$ 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
Code:
$ bash Sort_Script.sh Input_File  Output_File

Output is written to filename Output_File
Code:
$ cat Output_File
qeqe,ewqre,1,wqew
name,city,2,country


Last edited by Akshay Hegde; 11-05-2013 at 01:11 PM..
# 7  
Old 11-05-2013
Hi Akshay ,

I'm getting the below error

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

Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find the X highest values in a list depending on the values of another list with bash/awk?

Hi everyone, This is an exemple of inpout.txt file (a "," delimited text file which can be open as csv file): ID, Code, Value, Store SP|01, AABBCDE, 15, 3 SP|01, AABBCDE, 14, 2 SP|01, AABBCDF, 13, 2 SP|01, AABBCDE, 16, 3 SP|02, AABBCED, 15, 2 SP|01, AABBCDF, 12, 3 SP|01, AABBCDD,... (1 Reply)
Discussion started by: jeremy589
1 Replies

2. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

3. Shell Programming and Scripting

List unique values and count instances in .csv file

I need to take the second column of a .csv file and count the number of instances of each unique value in that same second column. I'd like the output to be value,count sorted by most instances. Thanks for any guidance! Data example: 317476,317756,0 816063,318861,0 313123,319091,0... (4 Replies)
Discussion started by: batcho
4 Replies

4. Shell Programming and Scripting

Sort csv file

Hello, I am facing the following problem: I have a file in csv format. So several records where data fields are separated by comma ',' The lenght of the fields is variable, as well as the number of fields per record. But there are at least 7 fields per record. I need to sort the file based... (1 Reply)
Discussion started by: BSF
1 Replies

5. Shell Programming and Scripting

return a list of unique values of a column from csv format file

Hi all, I have a huge csv file with the following format of data, Num SNPs, 549997 Total SNPs,555352 Num Samples, 157 SNP, SampleID, Allele1, Allele2 A001,AB1,A,A A002,AB1,A,A A003,AB1,A,A ... ... ... I would like to write out a list of unique SNP (column 1). Could you... (3 Replies)
Discussion started by: phoeberunner
3 Replies

6. Shell Programming and Scripting

sort the csv file

hello , the content of a csv file is looking like this... vah1 , imk4 , LDAP Secure. vah1 , fmk1 , Not able to SSH vah1 , fk1 ,Not able to SSH vah1 , imk1 , LDAP Secure. vah1 , imk3 , Un-Secured vah1 , fmk1 , LDAP Secure. vah1 , fk1 , LDAP Secure. vah1 , fmk1 , LDAP Secure. vah1 ,... (2 Replies)
Discussion started by: nagendramv
2 Replies

7. Shell Programming and Scripting

Need to compare two csv files values and write into another csv file

Hi all, Am new to scripting. So i just need your ideas to help me out. Here goes my requirement. I have two csv files 1.csv 2.csv abc,1.24 abc,1 def,2.13 def,1 I need to compare the first column of 1.csv with 2.csv and if matches then need to compare... (2 Replies)
Discussion started by: chinnahyd
2 Replies
Login or Register to Ask a Question