Sorting in groups


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting in groups
# 1  
Old 05-09-2015
Sorting in groups

Hi,

I am looking at a slightly different sorting problem and I am not sure how to do it in bash.

I have the following input:

Code:
0                                                                                                                                                                                                                                                                              
0.181017                                                                                                                                                                                                                                                                       
0.181017                                                                                                                                                                                                                                                                       
0.0409129                                                                                                                                                                                                                                                                      
0.214896                                                                                                                                                                                                                                                                       
0.214896                                                                                                                                                                                                                                                                       
0.781925                                                                                                                                                                                                                                                                       
1.05696                                                                                                                                                                                                                                                                        
1.05696                                                                                                                                                                                                                                                                        
0.791026                                                                                                                                                                                                                                                                       
1.10494                                                                                                                                                                             
1.10494                                                                                                                                                                             
2.04486                                                                                                                                                                             
9.37513                                                                                                                                                                             
9.37513
1.29588
15.6751
15.6751
15.873
19.2972
19.2972

I want to sort this, but in groups of three elements with the first one being the sorted value. For example line 16 is lesser than line 13 in the input and it must be re-sorted as the following. The two lines following the sorted value must be moved along with it.

Code:
0                                                                                                                                                                                                                                                                              
0.181017                                                                                                                                                                                                                                                                       
0.181017                                                                                                                                                                                                                                                                       
0.0409129                                                                                                                                                                                                                                                                      
0.214896                                                                                                                                                                                                                                                                       
0.214896                                                                                                                                                                                                                                                                       
0.781925                                                                                                                                                                                                                                                                       
1.05696                                                                                                                                                                                                                                                                        
1.05696                                                                                                                                                                                                                                                                        
0.791026                                                                                                                                                                                                                                                                       
1.10494                                                                                                                                                                             
1.10494                                                                                                                                                                             
1.29588
15.6751
15.6751
2.04486                                                                                                                                                                             
9.37513                                                                                                                                                                             
9.37513
15.873
19.2972
19.2972

Any pointers to do this is much appreciated.

Thanks!
Jamie
# 2  
Old 05-09-2015
Any attempts from your side?
# 3  
Old 05-09-2015
Hi,

I can print every third line and sort that very easily, using

Code:
awk 'NR%3==1' | sort -n

But I am not sure how to track the other two lines and add them to this sorted list..I am trying to figure a way out.
# 4  
Old 05-09-2015
Try
Code:
paste -s -d"\t\t\n" file2 | LC_ALL=C sort -n | tr '\t' '\n'
0
0.181017
0.181017
0.0409129
0.214896
0.214896
0.781925
1.05696
1.05696
0.791026
1.10494
1.10494
1.29588
15.6751
15.6751
2.04486
9.37513
9.37513
15.873
19.2972
19.2972

Hopefully the delimiters for paste and parameters for tr are accepted in this form. The LC_ALL=C is needed only if your locale setting has an undesirable influence on the sort command.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 05-09-2015
Assuming you don't care about preserving the hundreds of spaces at the ends of some of your input lines (since the spacing in your sample output lines does not match the spacing in your sample input lines), you could also try:
Code:
#!/bin/bash
while read a
do	read b
	read c
	printf '%s %s %s\n' "$a" "$b" "$c"
done < file | sort -n | while read a b c
do	printf '%s\n%s\n%s\n' "$a" "$b" "$c"
done

which strips off all of the trailing spaces.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 05-09-2015
Hi,

Thank you for the response. I am a bit confused on the intended usage. Does file2 mean another file which stores the unsorted entries? If I run it on the base file, the entries are not sorted at all.

Thanks.
# 7  
Old 05-09-2015
Quote:
Originally Posted by jamie_123
Hi,

Thank you for the response. I am a bit confused on the intended usage. Does file2 mean another file which stores the unsorted entries? If I run it on the base file, the entries are not sorted at all.

Thanks.
You didn't give us the name of your input file. So the filename used in responses to your request for help (file, file2, your_input_file, or RanDomStrIng) shouldn't matter. We assume that you know enough about shell scripting to be able to change the input file used in the sample solutions provided to be the name of your input file.

The suggestions RudiC and I provided do not modify your input file. The output sorted as you requested appears on standard output. You can pipe that output to another script or redirect that output to any other file that you want. Just do not redirect the output to your input file (or you will end up with an empty input file).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Groups

Must I be in a group? I am using Ubuntu and am the only user on my PC. I know how to change groups but do not see a way to not be in a group. Any help would be appreciated. (2 Replies)
Discussion started by: nthepines
2 Replies

2. Shell Programming and Scripting

Need help with regex groups

I have a requirement - replace specified positions in a string with a character. I found perl regex useful for this approach. however, I am facing the following issue. The target file 'temp' contains - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx The goal is to convert... (5 Replies)
Discussion started by: sam_roy
5 Replies

3. UNIX for Dummies Questions & Answers

Understanding Groups

Hi cat /etc/group : .... oinstall:x:401: dba:x:400:oracle ... cat /etc/passwd|grep oracle oracle:x:130:401::/home/oracle:/bin/ksh 1. Is that mean that : ORACLE user has OINSTALL as it Primary group and DBA as secondary group ? 2. What is the linux comman to set ORACLE user with... (2 Replies)
Discussion started by: yoavbe
2 Replies

4. Shell Programming and Scripting

Groups in Unix ???

What is Primary group and Secondary Group in Unix.? (1 Reply)
Discussion started by: gwgreen1
1 Replies

5. HP-UX

Groups access

Hi all, Can someone tell me how I can get around this problem. Basically I use the HP-UX OS and I work with 2 top level directories. /z/group1 /z/group2 these 2 dirs are managed where group1 can only be access by one set of users and group2 another. This is managed by adding the 2... (3 Replies)
Discussion started by: cyberfrog
3 Replies

6. Solaris

groups

1 user in member of 4 groups find file permissions and default group (1 Reply)
Discussion started by: tirupathi
1 Replies

7. Solaris

groups

how to create 1000 users in 1 group (0 Replies)
Discussion started by: tirupathi
0 Replies

8. Shell Programming and Scripting

groups starting with c2?

I have some groups and when i issue a command like groups $LOGNAME it displays in one line rfautosys c2ru cash2 I want to fetch only group starting with c2 but when i grep i am getting full line. Can someone advise on this please as how i can get output as c2ru? (2 Replies)
Discussion started by: gehlnar
2 Replies

9. Windows & DOS: Issues & Discussions

putty groups

Hi all, need info on using putty as group. I am having huge numbers of servers. (say 100) I am using putty to login remotely. i want to group each 25 hostnames or a set of servers into one putty instance. (see image attached.) Currently i have to scroll down to see all the 100 servers. ... (2 Replies)
Discussion started by: karthikn7974
2 Replies

10. AIX

Where are my groups

Hello A couple of weeks ago, I added a user to an AIX 5.3 system. I go to add one today, and it appears that when creating a user in smit, I cannot see any groups. No primary groups No Group set No Admin Groups The /etc/group and etc/secuity/group files seem to be intact. I did... (4 Replies)
Discussion started by: mhenryj
4 Replies
Login or Register to Ask a Question

Featured Tech Videos