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

File A

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]
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 ?

Hi

# 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.

1 Like
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]
1 Like