The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
need help listing/sorting files dave_angel Shell Programming and Scripting 2 11-23-2006 04:16 PM
Sorting files in a directory olimiles Shell Programming and Scripting 2 09-02-2006 10:03 AM
sorting dir with respect to their nr., of files atticus Shell Programming and Scripting 0 06-07-2006 03:05 PM
Sorting files sendhil Shell Programming and Scripting 4 02-16-2006 02:13 AM
sorting files Infraredskies UNIX for Advanced & Expert Users 4 10-25-2005 11:17 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-09-2006
Registered User
 

Join Date: Jun 2006
Posts: 29
Sorting Files

How to sort such files which contains records of varying length and varying lines? (With respect to Bash shell)

Eg:
Each record begins with a sting of 1/0(binary) which may or may not be followed by properties like AB,BS etc.
I have to sort such records on the basis of 1/0 string and keep the properties associated with that record intact.

Following lines are contained in the file

10101010101111101
AS sasa
BS kkk
1110000101010110
BS jsa
0110000101010100
1010000101010110
AS asas
BS sasa
CS asa

P.S: 1. A new record starts wherever a new line is starting with 1/0
2. Anyline starting with space is properties associated with the last
1/0 record
3. The number of records depicted in above file is 4




Please help in this regard.

Last edited by sandeep_hi; 06-09-2006 at 02:10 AM.
Reply With Quote
Forum Sponsor
  #2  
Old 06-09-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,297
Add a record separator = I used |, then roll the first records and sub-records into one line, sort, then unroll.
Code:
sed 's/^ /|/' filename | \
awk 'BEGIN{ getline; printf "%s", $0}
    { if(substr($0,1,1)=="|"){
         printf "%s", $0
      }
      else {
         printf "\n%s", $0
      }
    }    
     END{printf "\n"} 
     ' > newfile
sort newfile | sed 's/|/\n /g' > sortedfile
input
Code:
10101010101111101
 AS sasa
 BS kkk
1110000101010110
 BS jsa
0110000101010100
1010000101010110
 AS asas
 BS sasa
 CS asa
output
Code:
0110000101010100
1010000101010110
 AS asas
 BS sasa
 CS asa
10101010101111101
 AS sasa
 BS kkk
1110000101010110
 BS jsa
Reply With Quote
  #3  
Old 06-09-2006
Registered User
 

Join Date: Jun 2006
Posts: 29
Hi Jim !

Thanks alot...

Please tell me if you have any links for Shell Script beginer's

Regards
Sandeep

Last edited by sandeep_hi; 06-09-2006 at 05:45 AM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0