Group on the basis of common text in the square bracket and sorting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Group on the basis of common text in the square bracket and sorting
# 1  
Old 08-09-2010
Java Group on the basis of common text in the square bracket and sorting

File A

Code:
99   >ac [John 2]  >ss[John 2] >juk[John 2]
70   >acb[Mona 3.1] >defa[Mona 3.1]
90   >ca[John 2]
100  >aa[John 2] >abc[John 2]  >bca[John 2]
85  >cde[Mona 3.1]
81 >ghi[Rambo] >ghij [Rambo]
87 >def[Mona 3.1] >fgh[Mona 3.1] <ijk [Mona 3.1]
89  >fck[Rambo] >ghij[Rambo] >kill[Rambo] >aa[Rambo]

Code:
The given output shud be 
100  >aa[John 2] >abc[John 2]  >bca[John 2]
87 >def[Mona 3.1] >fgh[Mona 3.1] <ijk [Mona 3.1]
89  >fck[Rambo] >ghij[Rambo] >kill[Rambo] >aa[Rambo]

On the basis of text common within the square brackets , we can group accordingly and take one which have highest number in each group.

How can be done ?
# 2  
Old 08-09-2010
Hi

Code:
# sed 's/\]/\[/' file | sort -t"[" -k2,2 -k 1nr | awk -F"[" '!a[$2]++' | sed 's/\[/\]/2'
100  >aa[John 2] >abc[John 2]  >bca[John 2]
87 >def[Mona 3.1] >fgh[Mona 3.1] <ijk [Mona 3.1]
89  >fck[Rambo] >ghij[Rambo] >kill[Rambo] >aa[Rambo]
#

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 08-10-2010
MySQL

Code:
sed 's/\[/ \[/' al |sort -k4,1n -k1n >tmp
IFS=$'\n'
 for i in `cat tmp | cut -d"]" -f1 | sed 's/.*\[\(\.*\)/\1/'|uniq|sort`
    do
      sed "/$i/!d" tmp | sed -n '$p;s/ \[/[/'
    done
100  >aa [John 2] >abc[John 2]  >bca[John 2]
87 >def [Mona 3.1] >fgh[Mona 3.1] <ijk [Mona 3.1]
89  >fck [Rambo] >ghij[Rambo] >kill[Rambo] >aa[Rambo]

This User Gave Thanks to ygemici For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting on date basis

I have file data.txt having below data cat data.txt 01-MAY-13 2.38.11.00.100089 IN 4512 0000741881 01-MAY-13 2.38.11.00.100089 IN 4512 0000741881 01-JUN-13 2.38.11.00.100089 FC 1514 0000764631 01-NOV-13 2.38.11.00.100089 FC 1514 0000856571 01-NOV-13 2.38.11.00.100089 IN 300.32... (1 Reply)
Discussion started by: ranabhavish
1 Replies

2. Shell Programming and Scripting

Plz Help in sorting the data on date basis

I have file a.txt having below data cat a.txt 01-MAY-13 2.38.11.00.100089 IN 4512 0000741881 01-JUN-13 2.38.11.00.100089 IN 1514 0000764631 01-NOV-13 2.38.11.00.100089 IN 1514 0000856571 01-NOV-13 2.38.15.00.100015 IN 300.32 0000856531 01-JUN-13 2.38.19.00.100000 IN 2698 0000764493... (5 Replies)
Discussion started by: ranabhavish
5 Replies

3. UNIX for Dummies Questions & Answers

Plz Help in sorting the data on date basis

I have file having below data 01-MAY-13 2.38.11.00.100089 IN 4512 0000741881 01-JUN-13 2.38.11.00.100089 IN 1514 0000764631 01-NOV-13 2.38.11.00.100089 IN 1514 0000856571 01-NOV-13 2.38.15.00.100015 IN 300.32 0000856531 01-JUN-13 2.38.19.00.100000 IN 2698 0000764493 01-JUL-13... (2 Replies)
Discussion started by: ranabhavish
2 Replies

4. Shell Programming and Scripting

Doubt with regex to replace square bracket

there is a word "welcome" output should be "welcome\ i am using regsub to add backslash "\" in place where ever i find square brackets (open or close).. But i am not getting it... pls help out.. set a {welcome} set d (5 Replies)
Discussion started by: Syed Imran
5 Replies

5. Shell Programming and Scripting

ksh, difference between double bracket and single bracket

Can somebody tell me the difference between double brackets and single brackets, when doing a test. I have always been acustomed to using single brackets and have not encountered any issues to date. Why would somebody use double brackets. Ie if ] vs if Thanks to... (2 Replies)
Discussion started by: BeefStu
2 Replies

6. Shell Programming and Scripting

Delete text between square brackets and also delete those square brackets using sed or awk

Hi All, I have a text file which looks like this: computer programming systems engineering I want to get rid of these square brackets and also the text that is inside these brackets. So that my final text file looks like this: computer programming systems engineering I am using... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

7. Shell Programming and Scripting

perl : merging two arrays on basis of common parameter

I have 2 arrays, @array1 contains records in the format 1|_|X|_|ssd|_| 4|_|H|_|hbd|_| 9|_|Y|_|u8gjdfg|_| @array2 contains records in the format X|_|asdf|_| Y|_|qwer|_| A|_|9kdkf|_| @array3 should contain records in the PLz X|_|ssd|_|asdf|_| Y|_|hdb|_|qwer|_| PLZ dont use... (2 Replies)
Discussion started by: centurion_13
2 Replies

8. Shell Programming and Scripting

Expect Script square bracket dollar prompt

Hi Thanks for this amazing forum first, I've been searching answers in it for problems that I've encountered at work. The only problem I haven't been able to find a fix for, is a ever waiting for prompt problem in Expect when encounter a $ prompt. I usually set the timeout to -1 cause the... (2 Replies)
Discussion started by: Ikaro0
2 Replies

9. Shell Programming and Scripting

sorting of files on the basis of timestamp

Hi, With the help of below script im able to get the count of all the .xml files but that count is not specific to a day ie its the total count of all .xml files what i want is specific to 1 day and that of every half an hr ie from 23 feb 2009 7 am till 23rd feb 2009 2300 am and from 07:00 to... (1 Reply)
Discussion started by: ss_ss
1 Replies

10. Shell Programming and Scripting

Extract string in square bracket

Hi Input text is some message some message some message Expected output is main value1 value2 value3 Any idea how to above values in square brackets using shell scripting? many thanks. (3 Replies)
Discussion started by: hnh
3 Replies
Login or Register to Ask a Question