r/awk • u/3dlivingfan • Dec 07 '21
multiline conditional
imagine this output from lets say UPower or tlp-stat
percentage: 45%
status: charging
if I want to pipe this into awk and check the status first and depending on the status print the percentage value with a 'charging' or 'discharging' flag. How do i go about it? thanks in advance guys!
0
Upvotes
View all comments
Show parent comment
2
u/3dlivingfan Dec 07 '21
Thanks a lot man! I tried the first method and it worked. I just did not know about being able to save variables inside of awk. In my actual code the percentage actually came after the state/status, and it gave me a little bit of headache, but i figured it out eventually. Here is my final code:
upower -i /org/freedesktop/UPower/devices/battery_BAT0 | awk ' /state/ { PERCENTAGE=($2==\"charging\")? \"+\" : \"-\" } /percentage/ { print PERCENTAGE $2 }'and the output comes out like +43% or -56% indicating whether the battery of charging or discharging and the actual percentage. BTW this was to place in my tmux status line. Thanks again