#! /usr/bin/awk -f # When parsing 'upower --dump' /^Device:[ \t]+/ { device["path"] = $2 next } # When parsing 'upower --monitor-detail' /^\[[0-9]+:[0-9]+:[0-9]+\.[0-9]+\][ \t]+device changed:[ \t]+/ { device["path"] = $4 next } # BEGIN battery / battery/ && device["path"] { device["is_battery"] = 1 next } / state:/ && device["is_battery"] { device["battery_state"] = $2 next } / percentage:/ && device["is_battery"] { device["battery_percentage"] = $2 next } /^$/ && device["is_battery"] { printf("battery %s %s\n", device["battery_state"], device["battery_percentage"]) } # END battery # BEGIN line-power / line-power/ && device["path"] { device["is_line_power"] = 1 next } / online:/ && device["is_line_power"] { device["line_power_online"] = $2 next } /^$/ && device["is_line_power"] { printf("line_power %s\n", device["line_power_online"]) } # END line-power /^$/ { delete device next }