Sorting file based on a numeric column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting file based on a numeric column
# 1  
Old 10-23-2012
Sorting file based on a numeric column

Hi,

I use UBUNTU 12.04.

I have a file with this structure:
Code:
Name 2 1245787 A G 12 14 12 14 ....
Name 1 1245789 C T 13 12 12 12.....

I would like to sort my file based on the second column so to have this output for example:
Code:
Name 1 1245789 C T 13 12 12 12.....
Name 2 1245787 A G 12 14 12 14 ....

The second column goes from 1 to 22. I used sort -k2,2 file name but it doesn't sort my numbers in the second column but it gives 1 then 10, then 11 to 19, then 2 to 22.

How I should sort this, I imagine having numeric values in my second column is the problem.

Thank you very much.

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by radoulov; 10-23-2012 at 10:57 AM..
# 2  
Old 10-23-2012
have you tried sort -nk2,2 ?

Last edited by radoulov; 10-23-2012 at 10:58 AM..
# 3  
Old 10-23-2012
yes, I have -k2 and what it gives me is sorting like this:
1 then 10 (without 2 to 9)
then 11 to 19.
then 2, 20, 21 and 22.
then 3 to 9.
# 4  
Old 10-23-2012
There was a typo in my previous comment, you need to add option -n (for numeric sort)

Cheers Smilie
This User Gave Thanks to cabrao For This Post:
# 5  
Old 10-23-2012
Oh, Ok, I just got it. It works very well, thank you very much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting based on File name

Hi All I have a requirement to list all the files in chronological order based on the date value in the file name.For ex if I have three files as given below ABC_TEST_20160103_1012.txt ABC_TEST_20160229_1112.txt ABC_TEST_20160229_1112.txt I have written code as given below to list out... (2 Replies)
Discussion started by: ginrkf
2 Replies

2. Shell Programming and Scripting

[Solved] Sorting a column based on another column

hello, I have a file as follows: F0100010 A C F0100040 A G BTA-28763-no-rs 77.2692 F0100020 A G F0100030 A T BTA-29334-no-rs 11.4989 F0100030 A T F0100020 A G BTA-29515-no-rs 127.006 F0100040 A G F0100010 A C BTA-29644-no-rs 7.29827 F0100050 A... (9 Replies)
Discussion started by: Homa
9 Replies

3. Shell Programming and Scripting

[Solved] Sorting a column in a file based on a column in a second file

Hello, I have two files as the following: File1: F0100020 A G F0100030 A T F0100040 A G File2: F0100040 A G BTA-28763-no-rs 77.2692 F0100030 A T BTA-29334-no-rs 11.4989 F0100020 A G BTA-29515-no-rs 127.006 I want to sort the second file based on the... (6 Replies)
Discussion started by: Homa
6 Replies

4. Shell Programming and Scripting

Sorting file based on name

Hi team, We have few files landing to our server based on sequence number. These files have to be processed in the sequence number order. Once the sequence number has reached its maximum, the files with sequence number 0000 has to be processed. For example: IN9997 IN9998 IN9999 IN0000... (7 Replies)
Discussion started by: anijan
7 Replies

5. UNIX for Dummies Questions & Answers

Sorting a file based on the absolute value of a column

I would like to sort a tab delimited text file based on the absolute value of its second column. How do I go about doing that? Thanks! Example input: A -12 B 0 C -6 D 7 Output: A -12 D 7 C -6 B 0 (4 Replies)
Discussion started by: evelibertine
4 Replies

6. Shell Programming and Scripting

[SOLVED] Sorting into new files based on column

Hi, I have the following file, I need to sort it based on a column and write to different output files based on this column request_guid iso_country_cd address_data response_time 32895901-d17f-414c-ac93-3e7e0f5ec240 AND BaseName:CATALUNYA; HouseNumber:1; ISOCountryCode:AND;... (1 Reply)
Discussion started by: ramky79
1 Replies

7. Shell Programming and Scripting

sorting based on a specified column in a text file

I have a tab delimited file with 5 columns 79 A B 20.2340 6.1488 8.5086 1.3838 87 A B 0.1310 0.0382 0.0054 0.1413 88 A B 46.1651 99.0000 21.8107 0.2203 89 A B 0.1400 0.1132 0.0151 0.1334 114 A B 0.1088 0.0522 0.0057 0.1083 115 A B... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

8. Shell Programming and Scripting

sorting numeric array

Hi, I would like to do the following sorting, but the output is not what i expected. Why 222 and 2222 are not at the last two elements of array? awk 'BEGIN{a="22";a="2222";a="33";a="44";a="222";a="11";a="22";a="33";asort(a); for (i=1;i<=8;i++) print a}' 11 22 22 222 2222 33 33 44... (1 Reply)
Discussion started by: phoeberunner
1 Replies

9. Shell Programming and Scripting

How to check if a column is having a numeric value or not in a file?

Hi, I want to know, how we find out if a column is having a numeric value or not. For Example if we have a csv file as ASDF,QWER,GHJK,123,FGHY,9876 GHTY,NVHR,WOPI,623,HFBS,5386 we need to find out if the 4th and 6th column has muneric value or not. Thanks in advance Keerthan (9 Replies)
Discussion started by: keerthan
9 Replies

10. Shell Programming and Scripting

sorting csv file based on column selected

Hi all, in my csv file it'll look like this, and of course it may have more columns US to UK;abc-hq-jcl;multimedia UK to CN;def-ny-jkl;standard DE to DM;abc-ab-klm;critical FD to YM;la-yr-tym;standard HY to MC;la-yr-ytm;multimedia GT to KJ;def-ny-jrt;critical I would like to group... (4 Replies)
Discussion started by: tententen
4 Replies
Login or Register to Ask a Question