I have a problem with the output of a program. I need to launch a command in bash and take its output (a 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 500basically it is an xxx.yy.zz: value, but the value may contain spaces.Here's the output I'd like to getbattery.charge: 90battery.charge.low: 30battery.runtime: 3690battery.voltage: 230.0device.mfr: MGE UPS SYSTEMSdevice.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 a beginner.
How to split a string into an array in bash
bash;string;split
Pure bash solution, no external tools used to process the strings, just parameter expansion:#! /bin/bashstr='battery.charge: 90 battery.charge.low: 30 battery.runtime: 3690 battery.voltage: 230.0 device.mfr: MGE UPS SYSTEMS device.model: Pulsar Evolution 500'IFS=: read -a fields <<< $strfor (( i=0 ; i < ${#fields[@]} ; i++ )) ; do    f=${fields[i]}    notfirst=$(( i>0 ))    last=$(( i+1 == ${#fields[@]} ))    (( notfirst )) && echo -n ${f% *}    start=('' $'\n' ' ')    colon=('' ': ')    echo -n ${start[notfirst + last]}${f##* }${colon[!last]}doneechoExplanation: $notfirst and $last are booleans. The part before the last space ${f% *} isn't printed for the first field, as there is no such thing. $start and $colon hold various strings that separate the fields: at the first item, notfirst + last is 0, so nothing is prepended, for the rest of the lines, $notfirst is 1, so a newline is printed, and for the last line, the addition gives 2, so a space is printed. Then, the part after the last space is printed ${f##* }. Colon is printed for all lines except the last one.