Reputation: 87
Hi all im new in awk and cshell, can anyone know how to print the output by group like that ? My input file:
A 1
A 2
A 3
AD 4
AD 5
B 6
B 7
B 8
B 9
A 10
AC 11
AC 12
Output desire i want print by group if it have same in $1:
#########
#### A
#########
1
2
3
10
#########
#### AD
#########
4
5
#########
#### B
#########
6
7
8
9
#########
#### AC
#########
11
12
I have tried with this code:
set my_sort = `cat input | awk '{print $1}' |sort |uniq`
foreach name ($my_sort)
echo "##########"
echo "#### $name"
echo "##########"
awk '/^'$name'/ {print $0} input # i want print all line to check
end
but the output not like i want:
#########
#### A
#########
A 1
A 2
A 3
AD 4
AD 5
A 10
AC 11
AC 12
#########
#### AC
#########
AC 11
AC 12
#########
#### AD
#########
AD 4
AD 5
#########
#### B
#########
B 6
B 7
B 8
B 9
Any know where is my problem and can I optimal if can with awk ? Thanks all and have a nice day
Upvotes: 1
Views: 137
Reputation: 204310
$ sort -k1,1 -k2,2n file |
awk '$1!=prev{printf "########\n#### %s\n########\n", $1; prev=$1} {print $2}'
########
#### A
########
1
2
3
10
########
#### AC
########
11
12
########
#### AD
########
4
5
########
#### B
########
6
7
8
9
Upvotes: 2