The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to sort, and count unique data all at once? amatuer_lee_3 Shell Programming and Scripting 16 04-14-2009 10:22 PM
how to read the data from an excel sheet and use those data as variable in the unix c Anne Grace UNIX for Advanced & Expert Users 1 03-03-2008 07:21 AM
how to verify that copied data to remote system is identical with local data. ynilesh Shell Programming and Scripting 3 01-31-2008 08:55 AM
Script to sort data wizardy_maximus Shell Programming and Scripting 1 11-21-2007 04:30 AM
Howto capture data from rs232port andpull data into oracle database-9i automatically boss UNIX for Dummies Questions & Answers 1 09-23-2007 02:35 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-15-2005
bjorb bjorb is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 12
Red face sort data

Hi!
I'm trying to sort a file.dat with the sort command. The data contained by file.dat is similar to the data set below:

100.000
99.000
110.000

55.000
113.000
33.000

25.000
9.000
15.000

It is relatively easy to sort the data in ascending or descending order, but the problem is that the separating empty rows between the blocks are put on top of the sorted file. I wish to keep the empty rows and sort the data blocks separately, like this:

99.000
100.000
110.000

33.000
55.000
113.000

9.000
15.000
25.000

Can anybody please help me?
And also I must mention that I'm quite new at UNIX scripting, so try to explain in plain English !


bjorb
  #2 (permalink)  
Old 09-15-2005
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Got gawk?
Code:
gawk 'BEGIN{RS=""}{n=split($0,a);asort(a);for(i=0;i<=n;i++) print a[i] }' file.dat
  #3 (permalink)  
Old 09-15-2005
futurelet futurelet is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 137
Code:
ruby -00ne'puts split("\n").sort_by{|x|x.to_f};puts' file

Last edited by futurelet; 09-15-2005 at 02:23 AM.. Reason: a bit shorter
  #4 (permalink)  
Old 09-15-2005
bjorb bjorb is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 12
I'm too lazy

Hehe!
First of all I want to give my appreciation for a quick answer.

Without knowing what the heck that code from Ygor meant I tested it in my script. It seemed to work partially in my case, but as a result of my own lazyness it did not give me the result I need.

The data set is really in this format:

100.000 23.000 150.000
99.000 83.000 369.000
110.000 15.000 123.000

55.000 105.000 69.000
113.000 7.000 78.000
33.000 89.000 63.000

25.000 23.000 23.000
9.000 63.000 81.000
15.000 38.000 23.000


The columns represent x-, y- and z-coordinates.
I wish to sort the columns with x- and y-coordinates in ascending order.
Data is also supposed to be written back to data.dat
I apologize for any inconvenience and humbly ask you to help me again.

It is also in my interest to understand the prospective code which is given.
Where can I get a good tutorial on gawk?

Regards
bjorb
  #5 (permalink)  
Old 09-15-2005
futurelet futurelet is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 137
Quote:
Originally Posted by bjorb
The data set is really in this format:

100.000 23.000 150.000
99.000 83.000 369.000
110.000 15.000 123.000

55.000 105.000 69.000
113.000 7.000 78.000
33.000 89.000 63.000

25.000 23.000 23.000
9.000 63.000 81.000
15.000 38.000 23.000


The columns represent x-, y- and z-coordinates.
I wish to sort the columns with x- and y-coordinates in ascending order.
You mean change

Code:
100.000   23.000    150.000
99.000    83.000    369.000
110.000   15.000    123.000
to this?

Code:
99.000    15.000    159.000
100.000   23.000    369.000
110.000   83.000    123.000
  #6 (permalink)  
Old 09-15-2005
bjorb bjorb is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 12
simplified needs

After reviewing my needs I have realized that it would be sufficient to sort the data only considering the column with the y-coordinates:

From
100.000 23.000 150.000
99.000 83.000 369.000
110.000 15.000 123.000

to
110.000 15.000 123.000
100.000 23.000 150.000
99.000 83.000 369.000

How can I do this?

Regards

bjorb

Last edited by bjorb; 09-15-2005 at 08:04 AM.. Reason: Forgot to actually ask for help...
  #7 (permalink)  
Old 09-15-2005
futurelet futurelet is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 137
Code:
ruby -00ne'puts split("\n").sort_by{|x|x[/\s\S+/].to_f};puts' file
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:40 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0