![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
Code:
10101010101111101 AS sasa BS kkk 1110000101010110 BS jsa 0110000101010100 1010000101010110 AS asas BS sasa CS asa Code:
0110000101010100 1010000101010110 AS asas BS sasa CS asa 10101010101111101 AS sasa BS kkk 1110000101010110 BS jsa |
|
#3
|
|||
|
|||
|
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. |
|||
| Google The UNIX and Linux Forums |