LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-17-2012, 09:16 AM   #1
hanae
Member
 
Registered: May 2012
Posts: 33

Rep: Reputation: Disabled
Sort by the one of the columns at a time


I want to sort a file that I have by different sort criteria:
- in the alphabetic order first
- then group them by file name

This is the format of the file I have:

adfr file 5
axmll file 1
abcd file 4
xpmlm file 5
dmlp file 1

And I want to have two output files contain the results of the sorting
The first file like this:
abcd file 4
adfr file 5
axmll file 1
dmlp file 1
xpmlm file 5

The second file like this:
axmll file 1
dmlp file 1
abcd file 4
adfr file 5
xpmlm file 5

Can you please help me?
Thank you,
 
Old 05-17-2012, 10:00 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The sort command should do it flawlessly. What have you tried so far? sort alone (without options) sorts alphabetically starting from the first field, with -k option it sorts using the specified field. Option -n triggers numerical sorting. Please, see man sort for details.
 
Old 05-17-2012, 10:06 AM   #3
hanae
Member
 
Registered: May 2012
Posts: 33

Original Poster
Rep: Reputation: Disabled
I tried this this command:
- sort -s -n -k 1 filename, but it apparently, it doesn't recognize the first word as a column... can you please help?
 
Old 05-17-2012, 10:13 AM   #4
blue_print
Member
 
Registered: May 2010
Location: In world
Distribution: RHEL, CentOS, Ubuntu
Posts: 275
Blog Entries: 3

Rep: Reputation: 50
For first,

Code:
sort filename
For second

Code:
sort +2 -3 filename
 
Old 05-17-2012, 10:21 AM   #5
hanae
Member
 
Registered: May 2012
Posts: 33

Original Poster
Rep: Reputation: Disabled
Can you please explain what does this command mean:
sort +2 -3 filename

I tried it by it doesn't work, it just sort by the the filename?

I have the input like this

word filename token_number
 
Old 05-17-2012, 10:28 AM   #6
blue_print
Member
 
Registered: May 2010
Location: In world
Distribution: RHEL, CentOS, Ubuntu
Posts: 275
Blog Entries: 3

Rep: Reputation: 50
I just did try with yours,

Quote:
[root@server1 ~]# cat file
adfr file 5
axmll file 1
abcd file 4
xpmlm file 5
dmlp file 1
[root@server1 ~]# sort file
abcd file 4
adfr file 5
axmll file 1
dmlp file 1
xpmlm file 5
[root@server1 ~]# sort +2 -3 file
axmll file 1
dmlp file 1
abcd file 4
adfr file 5
xpmlm file 5

+2 - Sort with second line if 3rd column is same on more than one line
-2 - Do the sort based on 3rd column value
 
Old 05-17-2012, 10:32 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
It would be clearer using the option -k explicitly:
Code:
sort -k3n file
which is the same as
Code:
sort -n -k3 file
 
Old 05-17-2012, 10:46 AM   #8
hanae
Member
 
Registered: May 2012
Posts: 33

Original Poster
Rep: Reputation: Disabled
I still cannot get the good result I want

This is a sample of the file I have

Quote:
sky losn_revue-1981-2 40234
fly kivre1-0-0 1236
able udrum-umuray-13238-5 469878
after article1-1-1.2 13265
when I tried the command it is sorted by the last number which is token_num but not by filename?

normally the output should be:
Quote:
after article1-1-1.2 13265
fly kivre1-0-0 1236
sky losn_revue-1981-2 40234
able udrum-umuray-13238-5 469878
Thank you,
 
Old 05-17-2012, 11:05 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You have to run two different commands to get two different results! Or am I missing something?
Code:
$ cat file
sky losn_revue-1981-2 40234
fly kivre1-0-0 1236
able udrum-umuray-13238-5 469878
after article1-1-1.2 13265 
$ sort file
able udrum-umuray-13238-5 469878
after article1-1-1.2 13265 
fly kivre1-0-0 1236
sky losn_revue-1981-2 40234
$ sort -k3n file
fly kivre1-0-0 1236
after article1-1-1.2 13265 
sky losn_revue-1981-2 40234
able udrum-umuray-13238-5 469878
 
Old 05-17-2012, 11:07 AM   #10
blue_print
Member
 
Registered: May 2010
Location: In world
Distribution: RHEL, CentOS, Ubuntu
Posts: 275
Blog Entries: 3

Rep: Reputation: 50
Quote:
Originally Posted by hanae View Post
I still cannot get the good result I want

This is a sample of the file I have



when I tried the command it is sorted by the last number which is token_num but not by filename?

normally the output should be:


Thank you,

Well its working for me


Quote:
[root@server1 ~]# cat file1
sky losn_revue-1981-2 40234
fly kivre1-0-0 1236
able udrum-umuray-13238-5 469878
after article1-1-1.2 13265

[root@server1 ~]# sort +1 -2 file1
after article1-1-1.2 13265
fly kivre1-0-0 1236
sky losn_revue-1981-2 40234
able udrum-umuray-13238-5 469878
 
Old 05-17-2012, 11:19 AM   #11
hanae
Member
 
Registered: May 2012
Posts: 33

Original Poster
Rep: Reputation: Disabled
it is working for this specific case, but it is not with my whole file that contains thousands of file. AT a certain time the file is messed up.

aDuR Essai12-50-4 5273
ap CORPUS-131-18 5274
ira LMA_der12_20061219.058-29-13 5274
pymunt Doc_de_synthFse-127-9 5274
irumiYyn LKA_de_20061219.038-38-21 5275
issflid LKA_de_20061219.057-37-110 5275
rad Biographies1-92-3 5275
yxddm uddur-n-umur-105-5 5275
ayt LMA_BER_20061219.040-119-5 5276
i Livres1-70-5 5276
ittuyassann Reb1-49-13 5276
mani Fossman_revu-164-1 5276
n LMA_DE_20061219.020-42-100 5276
tn CORPUS-131-19 5277
a LMA_DE_20061219.058-29-14 5278
 
Old 05-18-2012, 04:18 AM   #12
hanae
Member
 
Registered: May 2012
Posts: 33

Original Poster
Rep: Reputation: Disabled
can someone help me figure out what is the problem??
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
text manipulation in bash: sort columns according to the first row lethalfang Linux - Newbie 5 06-20-2011 04:10 PM
sort multiple columns + replace another column cedance Linux - Newbie 1 03-29-2011 05:59 AM
[SOLVED] How to Sort Columns of Data (descending to ascending)? msbstar Linux - Newbie 5 03-21-2011 12:08 PM
[SOLVED] [Perl] fail to sort a file with 300,000 lines by multiple columns Kunsheng Programming 10 11-13-2009 06:41 PM
sort by multiple columns wakatana Linux - Newbie 5 10-18-2009 03:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration