Insert a header after each group of records.

Posted: December 2, 2016 in UNIX

I have a file like below. I want group on ID column and for each group i want add a header “#——–“‘.

ID|NAME|IP|
1|XX|10.16.240.48|No.1
1|YY|172.24.21.2|Yes.1.3
2|CC|172.24.21.137|RR.1
2|DD|172.24.21.18|YY.1
2|RR|10.16.200.30|CC.1
2|BB|10.16.200.31|DD.1
3|DD|10.16.200.32|FF.1
4|CC|10.16.200.45|GG.1
4|DD|10.16.200.33|HH.1
4|AA|10.16.200.34|RR.1
5|XX|10.16.200.46|XX.1
6|ZZ|10.16.200.35|DD.1
6|DD|172.24.203.201|YY.1
6|&&|10.16.200.12|HH.1
6|UU|172.24.212.27|II.1
6|II|10.16.230.43|OO.1

Output:-

1|AIXGOLD|10.16.240.48|AIX7.1
1|AKEPA|172.24.213.26|PowerHA7.1.3
#————-#
2|APROSE02|172.24.213.137|AIX7.1
2|APROSE03|172.24.213.138|AIX7.1
2|ATANDREW|10.16.240.30|AIX7.1
2|ATBOB|10.16.240.31|AIX7.1
#————-#
3|ATDOUG|10.16.240.32|AIX7.1
#————-#
4|ATMARSHA|10.16.240.45|AIX7.1
4|ATPERRY|10.16.240.33|AIX7.1
4|ATPJ|10.16.240.34|AIX7.1
#————-#
5|ATSTEVE|10.16.240.46|AIX7.1
#————-#
6|ATTRAIN|10.16.240.35|AIX7.1
6|BUELL|172.24.213.201|AIX7.1
6|BURNSIDE|10.16.240.12|AIX7.1
6|CARACARA|172.24.213.27|AIX7.1
6|DB2BOX|10.16.240.43|AIX7.1

Can use Awk command to archive the same

awk -F ‘|’ ‘{if (NR>1 && save!=$1) print “#————-#”;} {save=$1; print;}’ test.txt

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s