Issue
I get below data from my ansible playbook when I want to check service status:
host1 : zookeeper : Active: active (running) since Wed 2023-02-15 13:57:02 HKT; 2 days ago
host2 : zookeeper : Active: active (running) since Wed 2023-02-15 13:57:01 HKT; 2 days ago
host3 : zookeeper : Active: active (running) since Wed 2023-02-15 13:57:01 HKT; 2 days ago
host1 : Broker : Active: active (running) since Wed 2023-02-15 14:06:53 HKT; 1 day 23h ago
host2 : Broker : Active: active (running) since Wed 2023-02-15 14:07:33 HKT; 1 day 23h ago
host3 : Broker : Active: active (running) since Wed 2023-02-15 14:07:45 HKT; 1 day 23h ago
host4 : Broker : Active: active (running) since Wed 2023-02-15 14:07:52 HKT; 1 day 23h ago
host2 : SchemaRegistry : Active: active (running) since Wed 2023-02-15 14:13:41 HKT; 1 day 23h ago
host3 : SchemaRegistry : Active: active (running) since Wed 2023-02-15 14:15:04 HKT; 1 day 23h ago
host4 : Connect : Active: active (running) since Wed 2023-02-15 14:26:51 HKT; 1 day 23h ago
host1 : ControlCenter : Active: active (running) since Wed 2023-02-15 13:45:09 HKT; 2 days ago
host5 : KSQL : Active: active (running) since Wed 2023-02-15 14:20:28 HKT; 1 day 23h ago
host6 : KSQL : Active: active (running) since Wed 2023-02-15 14:20:28 HKT; 1 day 23h ago
host5 : RestProxy : Active: active (running) since Fri 2023-01-27 22:38:59 HKT; 2 weeks 6 days ago
host6 : RestProxy : Active: active (running) since Wed 2023-02-15 14:31:59 HKT; 1 day 23h ago
I then want to present this data in more readable format , so I want to print it as below
-------------------------------------------------------------------------------------------------
host1 : zookeeper : Active: active (running) since Wed 2023-02-15 13:57:02 HKT; 2 days ago
host2 : zookeeper : Active: active (running) since Wed 2023-02-15 13:57:01 HKT; 2 days ago
host3 : zookeeper : Active: active (running) since Wed 2023-02-15 13:57:01 HKT; 2 days ago
-------------------------------------------------------------------------------------------------
host1 : Broker : Active: active (running) since Wed 2023-02-15 14:06:53 HKT; 1 day 23h ago
host2 : Broker : Active: active (running) since Wed 2023-02-15 14:07:33 HKT; 1 day 23h ago
host3 : Broker : Active: active (running) since Wed 2023-02-15 14:07:45 HKT; 1 day 23h ago
host4 : Broker : Active: active (running) since Wed 2023-02-15 14:07:52 HKT; 1 day 23h ago
-------------------------------------------------------------------------------------------------
host2 : SchemaRegistry : Active: active (running) since Wed 2023-02-15 14:13:41 HKT; 1 day 23h ago
host3 : SchemaRegistry : Active: active (running) since Wed 2023-02-15 14:15:04 HKT; 1 day 23h ago
-------------------------------------------------------------------------------------------------
host4 : Connect : Active: active (running) since Wed 2023-02-15 14:26:51 HKT; 1 day 23h ago
-------------------------------------------------------------------------------------------------
host1 : ControlCenter : Active: active (running) since Wed 2023-02-15 13:45:09 HKT; 2 days ago
-------------------------------------------------------------------------------------------------
host5 : KSQL : Active: active (running) since Wed 2023-02-15 14:20:28 HKT; 1 day 23h ago
host6 : KSQL : Active: active (running) since Wed 2023-02-15 14:20:28 HKT; 1 day 23h ago
-------------------------------------------------------------------------------------------------
host5 : RestProxy : Active: active (running) since Fri 2023-01-27 22:38:59 HKT; 2 weeks 6 days ago
host6 : RestProxy : Active: active (running) since Wed 2023-02-15 14:31:59 HKT; 1 day 23h ago
-------------------------------------------------------------------------------------------------
I want to add a line after value in 2nd column changes. Could someone please help me with this?
Solution
Try something like this
awk -F: '{if($2!=p2){print "-------------------------";p2=$2} print}' file
edit
this code checks if the value of $2
is different than p2
then prints the separator and sets p2
to the new value and prints the input line.
Answered By - Diego Torres Milano Answer Checked By - Senaida (WPSolving Volunteer)