Sort the file contents in each group....print the group title as well


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort the file contents in each group....print the group title as well
# 8  
Old 08-25-2010
Then maybe your real input data is different from what you provided as sample, couse my code works OK on your sample data (at least in my environment).
# 9  
Old 08-25-2010
May be its environments... I am using the Solaris.
Code:
$ cat file1.txt
Reason : ABC
12345-0023
32123-5400
32442-5333

Reason : DEF
42523-3453
23345-3311

Reason : HIJ
454553-0001
$ awk -vRS="" '{system("echo \""$0"\" |sort -n -t- -k2");print ""}' file1.txt











$

Thanks for your quick reply.

---------- Post updated at 09:43 AM ---------- Previous update was at 09:39 AM ----------

Never mind Bartus..

I got it ...

awk -vRS="" '{system("echo \""$0"\" |sort -n -t- -k2");print}' file1.txt

Removed the "" after the print and it worked great.

Thanks ...
# 10  
Old 08-25-2010
Well in my Solaris 10 environment, i had to do this to make it work:
Code:
nawk  'BEGIN{RS=""}{system("echo \""$0"\" |sort -n -t- -k2");print ""}' file

This User Gave Thanks to bartus11 For This Post:
# 11  
Old 08-25-2010
Quote:
Originally Posted by prash184u
May be its environments... I am using the Solaris.
Always warn people of that first! Smilie
# 12  
Old 08-26-2010
perl may help you

Code:
local $/="\n\n";
while(<DATA>){
  my @tmp = split("\n",$_);
  print $tmp[0],"\n";
  print join "\n", map {$_->[0]} sort {$a->[1]<=>$b->[1]} map {my @t=split("-",$_);[$_,$t[1]]} @tmp[1..$#tmp];
  print "\n\n";
}
__DATA__
Reason : ABC
12345-0023
32123-5400
32442-5333

Reason : DEF
42523-3453
23345-3311

Reason : HIJ
454553-0001

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Sql ORA-00937: not a single-group group function

I'm trying to return only one row with the highest value for PCT_MAX_USED. Any suggestions? When I add this code, I get the ORA-00937 error. trunc(max(decode( kbytes_max, 0, 0, (kbytes_alloc/kbytes_max)*100))) pct_max_used This is the original and returns all rows. select (select... (3 Replies)
Discussion started by: progkcp
3 Replies

2. UNIX for Dummies Questions & Answers

How to create a volume group, logical volume group and file system?

hi, I want to create a volume group of 200 GB and then create different file systems on that. please help me out. Its becomes confusing when the PP calculating PP. I don't understand this concept. (2 Replies)
Discussion started by: kamaldev
2 Replies

3. Shell Programming and Scripting

need a one liner to grep a group info from /etc/group and use that result to search passwd file

/etc/group tiadm::345:mk789,po312,jo343,ju454,ko453,yx879,iy345,hn453 bin::2:root,daemon sys::3:root,bin,adm adm::4:root,daemon uucp::5:root /etc/passwd mk789:x:234:1::/export/home/dummy:/bin/sh po312:x:234:1::/export/home/dummy:/bin/sh ju454:x:234:1::/export/home/dummy:/bin/sh... (6 Replies)
Discussion started by: chidori
6 Replies

4. Shell Programming and Scripting

Sort Data by Group !

Hello, I have a file and i want to sort by third column and extract the three top lines of each group, it is determined by the second column (144, 89, 55, etc). Could you please help me with the appropiate awk shell script XLY-XLP 144 0.592772 XLY-XLE 144 0.798121 ... (3 Replies)
Discussion started by: csierra
3 Replies

5. Shell Programming and Scripting

Merge group numbers and add a column containing group names

Hi All I do have a file like this with 6 columns. Groups of data merge together and the group number is indicated above each group. 1 1 12 26 289 3.2e-027 GCGTATGGCGGC 2 12 26 215 6.7e+006 TTCCACCTTTTG 3 9 26 175 ... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

6. Shell Programming and Scripting

awk - sort, then print the high value for each group

Hi @ all I'm trying to achive to this problem, I've a 2-column composed file as the following: 192.168.1.2 2 192.168.1.3 12 192.168.1.2 4 192.168.1.4 3 cpc1-swan1-2-3-cust123.swan.cable.ntl.com 4 192.168.1.3 5 192.168.1.2 10 192.168.1.4 8... (8 Replies)
Discussion started by: m4rco-
8 Replies

7. Shell Programming and Scripting

Sort, group rows

I wrote script in bash which generates this report: User1,admin,rep,User2,shell,path1,x1,r1 User2,admin,rep,User7,shell,path1,x1,r1 User3,admin,rep,User4,shell,path1,x1,r1 User4,admin,rep,User3,shell,path1,x1,r1 User5,admin,rep,User1,shell,path1,x1,r1 User6,admin,rep,User5,shell,path1,x1,r1... (6 Replies)
Discussion started by: patrykxes
6 Replies

8. Shell Programming and Scripting

awk help required to group output and print a part of group line and original line

Hi, Need awk help to group and print lines to format the output as shown below INPUT FORMAT set echo on set heading on set spool on /* SCHEMA1 */ CREATE TABLE T1; /* SCHEMA1 */ CREATE TABLE T2; /* SCHEMA1 */ CREATE TABLE T3; /* SCHEMA1 */ CREATE TABLE T4; /* SCHEMA1 */ CREATE TABLE T5;... (5 Replies)
Discussion started by: rajan_san
5 Replies

9. UNIX for Dummies Questions & Answers

Sort cmd to get GROUP BY?

Is there any way using the sort command to get something like a GROUP BY clause? I'm trying to sort through race_event records and group them by those individuals that raced together in the same race on the same date at the same track, but i run into a problem when there were days when the track... (6 Replies)
Discussion started by: RacerX
6 Replies
Login or Register to Ask a Question