How to sort a column in excel/csv file?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to sort a column in excel/csv file?
# 1  
Old 04-05-2019
How to sort a column in excel/csv file?

I have to sort the 4th column of an excel/csv file. I tried the following command

sort -u --field-separator=, --numeric-sort -k 2 -n dinesh.csv > test.csv

But, it's not working. Moreover, I have to do the same for more than 30 excel/csv file. So please help me to do the same.
# 2  
Old 04-05-2019
Try:
Code:
sort -t, -k4,4n file

As to the -u switch, that does is not part of the standard when we are selecting fields.
This should work:
Code:
sort -t, -k4,4n file | uniq


Some sorts can do this:
Code:
sort -ut, -k4,4n file


--
Note: the csv format allows for field separators to be used as regular characters inside double quotes. If that is the case in your input file, then you need a bit of additional programming or a specialty program to do this..

Last edited by Scrutinizer; 04-05-2019 at 02:31 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-05-2019
Thanks Scrutinizer, Please help me to implement for multiple files. say for example, I would like to sort the 4 th column for 30 excel files.

--- Post updated at 05:29 AM ---

Thanks Scrutinizer, Please help me to do the same for multiple files. say for example, I would like to sort the 4 th column for 30 excel files.
# 4  
Old 04-05-2019
You're welcome. Try:
Code:
for file in *.csv
do
  sort -t, -k4,4n "$file" | uniq > "new_${file}"
done


--
Please note the note in post #2
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-05-2019
It works perfectly. Many many thanksSmilie
# 6  
Old 04-05-2019
Dear Scrutinizer,
I ran your script successfully for multiple files. But, right now the script is not working. Even I tried the following command separately on terminal
Code:
sort -t, -k4,4n myfile.csv > dinesh.csv

But, its not sorting now like earlier. It's a kind of mystery to understandSmilie
# 7  
Old 04-05-2019
Dear Scrutinizer,
It works with the following command,
Code:
sort -k4n,4 -t $'\t'

Smilie

Last edited by vbe; 04-05-2019 at 07:33 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to align/sort the column pairs of an csv file, based on keyword word specified in another file?

I have a csv file as shown below, xop_thy 80 avr_njk 50 str_nyu 60 avr_irt 70 str_nhj 60 avr_ngt 50 str_tgt 80 xop_nmg 50 xop_nth 40 cyv_gty 40 cop_thl 40 vir_tyk 80 vir_plo 20 vir_thk 40 ijk_yuc 70 cop_thy 70 ijk_yuc 80 irt_hgt 80 I need to align/sort the csv file based... (7 Replies)
Discussion started by: dineshkumarsrk
7 Replies

2. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

3. UNIX for Beginners Questions & Answers

How to insert data into black column( Secound Column ) in excel (.XLSX) file using shell script?

Source Code of the original script is down below please run the script and try to solve this problem this is my data and I want it column wise 2019-03-20 13:00:00:000 2019-03-20 15:00:00:000 1 Operating System LAB 0 1 1 1 1 1 1 1 1 1 0 1 (5 Replies)
Discussion started by: Shubham1182
5 Replies

4. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

5. Shell Programming and Scripting

Sort, sed, and zero padding date column csv bash scripting

Hello people, I am having problem to sort, sed and zero padding of column in csv file. 7th column only. Input of csv file: 1,2,3,4,5,6,4/1/2010 12:00 AM,8 1,2,3,4,5,6,3/11/2010 9:39 AM,8 1,2,3,4,5,6,5/12/2011 3:43 PM,8 1,2,3,4,5,6,12/20/2009 7:23 PM,8 Output:... (5 Replies)
Discussion started by: sean1357
5 Replies

6. UNIX for Dummies Questions & Answers

Sort csv file by duplicated column value

hello, I have a large file (about 1gb) that is in a file similar to the following: I want to make it so that I can put all the duplicates where column 3 (delimited by the commas) are shown on top. Meaning all people with the same age are listed at the top. The command I used was ... (3 Replies)
Discussion started by: jl487
3 Replies

7. Shell Programming and Scripting

Sort Excel File

Hi, I am new to scripting. I have a requirement 1.convert excel 2007 format to excel 2003 fromat 2.sort the excel file -( all columns in descending order). This needs to be done progarmmatically. Please let me know what would be the possible steps that I would need to take inorder to... (2 Replies)
Discussion started by: eva13
2 Replies

8. UNIX for Dummies Questions & Answers

Excel File to CSV

Hi All, I have to convert the excel file which will be placed in the Unix box to a CSV file using a shell script. Please Advise. Thanks & Regards, Kumar66 (1 Reply)
Discussion started by: kumar66
1 Replies

9. Shell Programming and Scripting

How to sort columns in excel(csv) file

i want sort columns with headers based on another file headers file1 eg: i'm having an empty file with only coumn names like lastname firstname title expirydate stlcno status etc... another file with same column names and some other as well but in different order... file2 eg:firstname... (2 Replies)
Discussion started by: Man83Nagesh
2 Replies
Login or Register to Ask a Question