Sorting blocks of data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting blocks of data
# 1  
Old 07-03-2007
Error Sorting blocks of data

Hello all,

Below is what I am trying to accomplish:

I have a file that looks like this

Code:
/* ----------------- xxxx.y_abcd_00000050 ----------------- */ 

jdghjghkla
sadgsdags
asdgsdgasd
asdgsagasdg

/* ----------------- xxxx.y_abcd_00000055 ----------------- */ 

sdgsdg
sdgxcvzxcbv
rtertwre
tqwetqe
xzcbvzxcb

/* ----------------- xxxx.y_abcd_00000005 ----------------- */ 

sdgsdg
sdgxcvzxcbv
rtertwre
tqwetqe
xzcbvzxcb

I need to sort it so that it looks like this

Code:
/* ----------------- xxxx.y_abcd_00000005 ----------------- */ 

sdgsdg
sdgxcvzxcbv
rtertwre
tqwetqe
xzcbvzxcb

/* ----------------- xxxx.y_abcd_00000050 ----------------- */ 

jdghjghkla
sadgsdags
asdgsdgasd
asdgsagasdg

/* ----------------- xxxx.y_abcd_00000055 ----------------- */ 

sdgsdg
sdgxcvzxcbv
rtertwre
tqwetqe
xzcbvzxcb

So i really want to sort only based on the items within /*----*/

I looked up the forums but could not find anything....any suggestions?
# 2  
Old 07-03-2007
Alfredo,
Even though your output file does not match with your requirement,
here is how you can keep the dashes and sorting what is inside of it:
Code:
mSorted='Sorted.txt'
rm -f $mSorted
csplit -k b "/-------/" {99}
for mFile in xx??
do
  sort $mFile >> $mSorted
done

# 3  
Old 07-03-2007
Thanks for you response..on trying that this is the error I get
csplit: b: No such file or directory
sort: open failed: xx??: No such file or directory??

Let me rephrase the question and post the actual file that needs to be sorted:

Code:
/* ----------------- SVAMN14GLBSMGR.c_imds_00000060 ----------------- */ 

insert_job: SVAMN14GLBSMGR.c_imds_00000060   job_type: c 
command: script.sh par1 par2
machine: SVAMN14GLBSMGR
permission: gx,wx

/* ----------------- SVAMN14GLBSMGR.c_imds_00000050 ----------------- */ 

insert_job: SVAMN14GLBSMGR.c_imds_00000050   job_type: c 
command: script.sh par1 par2
machine: SVAMN14GLBSMGR
permission: gx,wx


/* ----------------- SVAMN14GLBSMGR.c_imds_00000055 ----------------- */ 

insert_job: SVAMN14GLBSMGR.c_imds_00000055   job_type: c 
command: script.sh par1 par2
machine: SVAMN14GLBSMGR
permission: gx,wx

This needs to be sorted to:

Code:
/* ----------------- SVAMN14GLBSMGR.c_imds_00000050 ----------------- */ 

insert_job: SVAMN14GLBSMGR.c_imds_00000050   job_type: c 
command: script.sh par1 par2
machine: SVAMN14GLBSMGR
permission: gx,wx

/* ----------------- SVAMN14GLBSMGR.c_imds_00000055 ----------------- */ 

insert_job: SVAMN14GLBSMGR.c_imds_00000055   job_type: c 
command: script.sh par1 par2
machine: SVAMN14GLBSMGR
permission: gx,wx

/* ----------------- SVAMN14GLBSMGR.c_imds_00000060 ----------------- */ 

insert_job: SVAMN14GLBSMGR.c_imds_00000060   job_type: c 
command: script.sh par1 par2
machine: SVAMN14GLBSMGR
permission: gx,wx

How would I go abt doing that?
# 4  
Old 07-03-2007
Alfredo, 'b' is my test file -- sorry about it.
In your case, please, replace:
Code:
csplit -k b "/-------/" {99}

By:
Code:
csplit -k your_file "/-------/" {99}

# 5  
Old 07-03-2007
I got it to work...however I think you might have misunderstood my requirement. I need to sort blocks based on the first line. THe data within the block needs to remain the same --should not change. SO below only the items in bold are what I am concerned about...

So if I have 3 lines

/---S005---/asd
dfasd
asdg

/---S003---/erqwe
dvasd
xcvc

/---S001---/ewrwqer
dsaf
xzcvxc

this needs to be sorted to

/---S001---/
dsaf
xzcvxc

/---S003---/
dvasd
xcvc

/---S005---/
dfasd
asdg

Last edited by alfredo123; 07-03-2007 at 06:15 PM..
# 6  
Old 07-04-2007
alternate soln

Code:
paste -d "\t\t\t\t\t\t\n" -s test_sort_file|sort -t_ -n -k3,3 | tr "\t\t" "\n"


Last edited by ranj@chn; 07-04-2007 at 08:59 AM.. Reason: typo
# 7  
Old 07-04-2007
You can try something like that :
Code:
awk '
   /^\/\* ---.*--- \*\// && NF==5 { hdr=$3}
   { printf("%s.%09d\t%s\n", hdr ,NR, $0) }
' sort_blk.txt | sort -k1,1 | cut -f2-

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sorting blocks by a section of the identifier

I have a file that should be sorted by a string (shown in red in my example below) in the identifier. The RS is ^@M0, something like this: @M04961:22:000000000-B5VGJ:1:1101:9280:7106 1:N:0:86 GGCATGAAAACATACAAACCGTCTTTCCAGAAATTGTTCCAAGTATCGGCAACAGCTTTATCAATACCATGAAAAATATCAACCACACCAGAAGCAGCAT... (16 Replies)
Discussion started by: Xterra
16 Replies

2. UNIX for Dummies Questions & Answers

Delete data blocks based on missing combinations

Hello masters, I am filtering data based on completeness. A (Name , Group) combination in File2 is only complete when it has data for all subgroups specified in File1. All incomplete (Name , Group) combinations do not appear in the output. So for example , Name1 Group 1 in File2 is... (6 Replies)
Discussion started by: senhia83
6 Replies

3. Shell Programming and Scripting

Row blocks to column blocks

Hello, Searched for a while and found some "line-to-column" script. My case is similar but with multiple fields each row: S02 Length Per S02 7043 3.864 S02 54477 29.89 S02 104841 57.52 S03 Length Per S03 1150 0.835 S03 1321 0.96 S03 ... (9 Replies)
Discussion started by: yifangt
9 Replies

4. Shell Programming and Scripting

Difficult transposing of data from profiles blocks

Hello to all, I really hope some expert or awk guru could help me with this. I don't have how to begin and hope is not so difficult for somebody. I'll expecting how someone could resolve this problem I have to parse this. I have blocks of parameters for each MSISDN and I would like to extract... (9 Replies)
Discussion started by: Ophiuchus
9 Replies

5. Shell Programming and Scripting

Extracting data blocks from file

Hi all, I want to extract blocks of data from a file depending on the contents of that block. The input file(table) has several blocks each starting with 'gene' in the first column. I want to extract only those blocks which do not have the expression '_T02' in the second column. Input file ... (3 Replies)
Discussion started by: newbie83
3 Replies

6. Shell Programming and Scripting

how to split this file into blocks and then send these blocks as input to the tool called Yices?

Hello, I have a file like this: FILE.TXT: (define argc :: int) (assert ( > argc 1)) (assert ( = argc 1)) <check> # (define c :: float) (assert ( > c 0)) (assert ( = c 0)) <check> # now, i want to separate each block('#' is the delimeter), make them separate files, and then send them as... (5 Replies)
Discussion started by: paramad
5 Replies

7. UNIX for Dummies Questions & Answers

Convert 512-blocks to 4k blocks

I'm Unix. I'm looking at "df" on Unix now and below is an example. It's lists the filesystems out in 512-blocks, I need this in 4k blocks. Is there a way to do this in Unix or do I manually convert and how? So for container 1 there is 7,340,032 in size in 512-blocks. What would the 4k block be... (2 Replies)
Discussion started by: rockycj
2 Replies

8. UNIX for Dummies Questions & Answers

Sorting data

Hello guys. I need help figuring this one out. It's probably really easy. Thanks in advance! I have a file say for example containing this: Rice Food Carrots Food Beans Food Plates Kitchen Fork Kitchen Knives Kitchen I need: Food Rice, Carrots, Beans Kitchen Plates, Fork,... (7 Replies)
Discussion started by: visuelz
7 Replies

9. UNIX for Dummies Questions & Answers

Sorting data from a to z

Hi, Let's say I have these 3 columns; NGC1234 6 9 SL899 4 1 NGC1075 8 3 SL709 5 2 And I want to sort the data according to the first column (from a to z) like having them as: NGC1075 8 3 NGC1234 6 9 SL709 5 2 SL899 4 1 Can that be done... (2 Replies)
Discussion started by: cosmologist
2 Replies

10. Shell Programming and Scripting

Delete blocks with no data..

Hi, I tried this but could not get it... here is what I need I have an xml where I get all the data in blocks but some times I get empty blocks with no data...shown below..I need to delete only those blocks with no data, I tried couple of ways but could not do it..any help is appreciated...... (1 Reply)
Discussion started by: mgirinath
1 Replies
Login or Register to Ask a Question