Visit Our UNIX and Linux User Community


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
# 1  
Old 08-24-2010
Sort the file contents in each group....print the group title as well

I've this file and need to sort the data in each group

File would look like this ...

cat file1.txt
Code:
Reason : ABC
12345-0023
32123-5400
32442-5333

Reason : DEF
42523-3453
23345-3311

Reason : HIJ
454553-0001

I would like to sort each group on the last 4 fileds and print them in the same group

I want the file to look like this

Code:
Reason : ABC
12345-0023
32442-5333
32123-5400


Reason : DEF
23345-3311
42523-3453


Reason : HIJ
454553-0001

I tried using the sort command but it would mess up the group.. any hints or ideas would be appriciated too....

Thanks in advance
# 2  
Old 08-24-2010
Before you can sort it, you must first break them into separate groups.

---------- Post updated at 01:57 PM ---------- Previous update was at 01:46 PM ----------

This is a bit of a kludge since the sort command doesn't seem to be able to sort based on anything but fixed column positions, and your first column is variable size. If we swap the data around, it can always sort based on the first four columns.

Code:
#!/bin/sh

while read LINE
do
        # First line of a group
        if [ "${LINE:0:6}" == "Reason" ]
        then
                # Print the header
                echo "${LINE}"
                # Keep looping until we hit a blank line.
                while IFS="-" read A B && [ ! -z "${A}" ]
                do
                        # Reverse the order of the items so they can be sorted
                        echo "${B}-${A}"
                done | sort -u --key=1,4 |
                # Reverse the order back
                while IFS="-" read B A
                do
                        echo "${A}-${B}"
                done

                # Add a blank line to the end.
                echo
        fi
done < data

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 08-24-2010
Code:
awk -vRS="" '{system("echo \""$0"\" |sort -n -t- -k2");print ""}' file

# 4  
Old 08-24-2010
Another way:
Code:
sed '/^$/d' TestGroup.txt | \
gawk 'BEGIN{arrayIndex=0}
{
	if($1 == "Reason")
	{
		if(arrayIndex != 0)
		{
			arrayElements = asort(myArray)
			for(i=1; i <= arrayElements; i++)
			{
				print myArray[i]
			}
		}
		print $0
		arrayIndex=0
		delete myArray
	}
	else
	{
		myArray[arrayIndex]=$0
		arrayIndex++
	}
}END{
		if(arrayIndex != 0)
		{
			arrayElements = asort(myArray)
			for(i=1; i <= arrayElements; i++)
			{
				print myArray[i]
			}
		}
	}'

This User Gave Thanks to felipe.vinturin For This Post:
# 5  
Old 08-24-2010
MySQL

Code:
while read -r l; do if [[ "$l" != "" ]] ; then echo "$l">>tmp ; else sort -t- -k2n tmp; echo "" ; rm -f tmp ; fi ; done<infile ; more tmp ; rm -f tmp
Reason : ABC
12345-0023
32442-5333
32123-5400
 
Reason : DEF
23345-3311
42523-3453
 
Reason : HIJ
454553-0001

This User Gave Thanks to ygemici For This Post:
# 6  
Old 08-24-2010
Ah, so setting RS="" makes awk break records apart on blank lines. So you can just read in records one by one and sort them. Nice!
# 7  
Old 08-25-2010
Quote:
Originally Posted by ygemici
Code:
while read -r l; do if [[ "$l" != "" ]] ; then echo "$l">>tmp ; else sort -t- -k2n tmp; echo "" ; rm -f tmp ; fi ; done<infile ; more tmp ; rm -f tmp
Reason : ABC
12345-0023
32442-5333
32123-5400
 
Reason : DEF
23345-3311
42523-3453
 
Reason : HIJ
454553-0001

Thanks you all.

But this one was perfect.

You guys are the best.

---------- Post updated 08-25-10 at 09:23 AM ---------- Previous update was 08-24-10 at 04:23 PM ----------

A good question to Bartus...

I tried your command as well but it prints nothing but the blank lines ...

Can you please explain the command or fix it please?

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

Featured Tech Videos