Skip to main content
1 of 4
user67524
  • 83
  • 1
  • 1
  • 3

How to split such a string into array in bash

I have problem with output of a program. I need to launch a command in bash and take its ouput (string) and split it to add new lines in certain places. The string looks like this:

battery.charge: 90 battery.charge.low: 30 battery.runtime: 3690 battery.voltage: 230.0 device.mfr: MGE UPS SYSTEMS device.model: Pulsar Evolution 500

basically it's xxx.yy.zz: value, but value may contain spaces. Here's the output I'd like to get

battery.charge: 90
battery.charge.low: 30
battery.runtime: 3690
battery.voltage: 230.0
device.mfr: MGE UPS SYSTEMS
device.model: Pulsar Evolution 500 

I have an idea to search for first dot and then look back from that position for space to put a new line there, but I'm not sure how to achieve it in Bash. I'm still beginner. Please, help me with it.

user67524
  • 83
  • 1
  • 1
  • 3